2
我有一個QML C++項目,其中C++部分綁定後端和QML用戶界面之間的連接。覆蓋QML文件中設置的ListModel
我設置了一個QObject的子類,它有一個QAbstractListModel屬性作爲上下文屬性。
我的一個組件在qml文件中預定義了一個List模型。我想用我自己的列表模型替換它。但是如果context屬性沒有設置,我想保留這個模型。這使我可以在沒有C++部分的情況下運行本程序。 將模型設置爲上下文屬性並未進行剪切,因爲本地模型會超出上下文屬性。
我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
}
}
}
是否可以兼得,在QML文件模型在設計和GUI測試和疼痛自由改寫默認值的顯示,如果我設置myObject的作爲上下文屬性?
編輯:我使用QT 4 QtQuick 1.1
謝謝,這是非常有用的。 爲了保持在編輯器中看到默認數據的能力,我在onCompleted塊之前添加了'model = myModel'這一行 – 2014-12-04 09:05:18