[英]Overwrite ListModel set in QML file
I have a QML C++ Project where the C++ Part ties the connection between a backend and the QML user interface. 我有一个QML C ++项目,其中C ++部分将后端和QML用户界面之间的连接联系在一起。
I set an subclass of QObject, which has an QAbstractListModel property, as context property. 我将具有QAbstractListModel属性的QObject子类设置为上下文属性。
One of my components has a List model predefined in the qml file. 我的组件之一在qml文件中预定义了一个列表模型。 And i would like to replace that with my own list model.
我想用我自己的列表模型替换它。 But i want to keep that model if the context property is not set.
但是如果上下文属性未设置,我想保留该模型。 That allows me to run the Program without the c++ part.
这使我可以在不使用c ++部分的情况下运行程序。 Setting the model as context property didn't do the cut, because the local Model overuled the context property.
将模型设置为context属性并不能解决问题,因为本地Model覆盖了context属性。
My QML looks like that 我的QML像这样
Rectangle {
id: root_rect
objectName: "root_rect"
width: 300
height: 300
color: "#dbdbdb"
ListModel {
id: myModel
ListElement {
name: "foo1"
fin: "bar1"
}
ListElement {
name: "foo2"
fin: "bar2"
}
}
Rectangle {
id: list_bg
color: "#ffffff"
clip: true
anchors.top: parent.top
anchors.topMargin: 10
anchors.bottom: parent.bottom
anchors.bottomMargin: 10
anchors.left: parent.left
anchors.leftMargin: 10
anchors.right: parent.right
anchors.rightMargin: 10
ListView {
id: list_view1
anchors.fill: parent
delegate: Item {
x: 5
height: 40
Row {
id: row1
spacing: 10
Text {
text: name+" "+fin
anchors.verticalCenter: parent.verticalCenter
font.bold: true
}
}
}
model: myModel
//model: myObject.myModel
}
}
}
Is it possible to have both, Model in qml File for display of default values in Designer and for Gui testing, and pain free overwriting if i set that myObject as context property? 是否可以同时存在qml文件中的Model以便在Designer中显示默认值并进行Gui测试,并且如果我将myObject设置为上下文属性,可以轻松进行覆盖?
Edit: i use QT 4 with QtQuick 1.1 编辑:我将QT 4与QtQuick 1.1一起使用
I don't know if this work with QtQuick 1, but you could rely on exceptions handling. 我不知道QtQuick 1是否可以使用此功能,但是您可以依靠异常处理。 Something like this works with QtQuick 2:
像QtQuick 2这样的东西:
ListView {
id: list_view1
anchors.fill: parent
delegate: Item {
...
}
model: myModel
Component.onCompleted:{
try{
model = myObject.myModel
}
catch(exception){
console.log("myObject unknown, switching to default model")
model = myModel
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.