在QML中动态创建ListModel [英] Dynamically create ListModel in QML
问题描述
当我需要在运行时创建任何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({})时,我必须为内部模型传递新创建的ListModel。我不能在外部委托中使用静态定义的内部模型,因为我无法在运行时访问此类模型。顺便说一下,它可以以某种方式访问吗?
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?
P.S。也许在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!