Qml 我可以将模型列作为属性传递吗
是否可以将模型列名作为属性传递,而不是text:name,是否可以动态分配模型列名Qml 我可以将模型列作为属性传递吗,qml,Qml,是否可以将模型列名作为属性传递,而不是text:name,是否可以动态分配模型列名 ListView { model: dataModel delegate: Column { id: delegate Row { Repeater { model: 1 ItemDelegate { id:columnDelegate
ListView {
model: dataModel
delegate: Column {
id: delegate
Row {
Repeater {
model: 1
ItemDelegate {
id:columnDelegate
text:name
}
}
}
}
}
这样的事情是可能的?有什么办法解决这个问题吗?
我可以将模型列名作为数组传递吗?如果您正在使用数组数据,并且希望在
中继器中将其作为模型使用,则可以执行以下操作:
ListView {
property var columnName //pass column name as property
model: dataModel
delegate: Column {
id: delegate
Row {
Repeater {
model: 1
ItemDelegate {
id:columnDelegate
text:columnName //assign property as column name here
}
}
}
}
}
取自
如果您有来自web服务器的更深入的数据,例如JSON,您还可以查看以下内容
Column {
Repeater {
model: ["apples", "oranges", "pears"]
text: modelData
}
}
如果您的数据是更详细的对象,这非常适合访问属性,或者,如果它是静态的(如第一个示例),或者是简单的数组。太好了
我建议你看看其他的例子来帮助你
希望有帮助 QML使用模型的方式是使用角色。您应该使用modelData。
并且从您的模型中,您应该根据角色返回不同的数据(假设您正在实现qabstractemmodel
),如果您希望为文本
源使用数组数据,请查看通过数组的循环,以分配文本
,或者在这里查看模型文档
property var arrayData: {apples: {type: "fruit", colour: "green"}, oranges: {type: "fruit", colour: "orange"}, pears: {type: "fruit", colour: "green"}}
model: Object.keys(arrayData) //the 'key' (apples/oranges/pears)
text: arrayData.type // all would be 'fruit' in this example
detailText: arrayData.colour // moreso is you are using a `SimpleRow` to display your data`, but an example of usage