Dynamically create ListModel in QML(在 QML 中动态创建 ListModel)
问题描述
当我需要在运行时创建任何 QML 组件时,我可以使用该指南:http://qt-project.org/doc/qt-5/qtqml-javascript-dynamicobjectcreation.html
When I need to create any QML component in runtime, I can use that guide: http://qt-project.org/doc/qt-5/qtqml-javascript-dynamicobjectcreation.html
即只需调用 Qt.createComponent 和 component.createObject
i.e. just call Qt.createComponent and component.createObject
但我找不到如何在运行时创建 ListModel?使用 qml,而不是在 C++ 中.
But I couldn't find how to create ListModel at runtime? with qml, not in c++.
你可以问,我为什么需要它.所以,我有一个嵌套的 ListModel:有 外层模型,它的代表包含 内层模型.所以当我调用outer_model.append({}) 时,我必须为inner model 传递新创建的ListModel.我不能在外部委托中使用静态定义的 inner model,因为我无法在运行时访问这样的模型.对了,能不能通过某种方式访问?
You can ask, why I need it. So, I have a nested ListModel: there is outer model, which delegates contained inner models. So when I'm calling outer_model.append({}), I must pass newly created ListModel for inner model. I cannot use statically defined inner model in outer delegate, because I cannot access such model in runtime. By the way, can it be accessed somehow?
附:也许尝试在 javascript 中管理模型是完全错误的想法?
P.S. Maybe it's completely wrong idea to try managing models in javascript?
推荐答案
试试这个:
Component {
id: someComponent
ListModel {
}
}
function createModel(parent) {
var newModel = someComponent.createObject(parent);
return newModel;
}
这篇关于在 QML 中动态创建 ListModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 QML 中动态创建 ListModel


基础教程推荐
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01