【发布时间】:2017-12-22 17:42:52
【问题描述】:
我想知道如何在 ListModel 中传递一个数组?
好的,在 QML 中,我有一个 ListView,我将其设置为 ListModel,如下所示:
model: ListModel
{
id: myList
ListElement
{
name: ""
card: 0
books: []
}
}
我可以使用:
myList.append({name:"terry", card:00100, books:["024589","865976","879582","215645"]});
但是当我尝试在屏幕上输出它时,我得到了这个。
{
"card": 00100
"books": {
"objectName": "",
"count": 4,
"dynamicRoles": false
},
"name": "terry",
"name": "terry"
}
我不知道为什么我会得到 2 个名字!以及如何获得书籍的价值?
我查阅了ListModel 和ListElement 的QML 文档,找不到任何与传递数组相关的内容,所有示例都是整数或字符串。
知道如何获取日期吗?
我确实通过使用Component.onCompleted:{} 调用 Delegate 中的数组来解决它,但我认为这不是一个好的/正确的方法,因为 Delegate 不负责保存数据并且应该在模型中完成,如果我错了,请纠正我。
感谢您的宝贵时间。
Edit01:感谢您的回复,这是我需要数组的原因: 我在 Delegate 中有一个 ComboBox,如下所示:
delegate: Rectangle
{
id: rowID
width: 50
height: 40
color: "#323232"
Row
{
anchors.fill: parent
anchors.leftMargin: 10
anchors.rightMargin: 10
Label{
id: nameID
text: name
font.pixelSize: 12
width: 200
wrapMode: Text.WrapAnywhere
anchors.verticalCenter: parent.verticalCenter
color: "#999"
}
Label{
anchors.verticalCenter: parent.verticalCenter
text: "out:"
font.pixelSize: 12
color: "#999"
}
ComboBox{
id: booksID
height: 20
width: 50
model: books
anchors.verticalCenter: parent.verticalCenter
}
}
}
如您所见,我将 name 提供给 Label(id: nameID)并且我想提供 book 到 ComboBox (id: booksID) 具有 model,如果我将 books 键设为 ListElement 如何提供所有值?
在 QML ListModel 或 ListElement 文档中没有提到任何关于获取所有密钥值的内容吗?它只支持基于索引号的get(int index)。
【问题讨论】:
-
只是快速浏览文档的猜测,在模型中尝试
books: [ ListElement "" ]。 -
@T4rk1n 我得到了这个
UiError: widget creation failed但是当我使 ListElement 等于 JSON 时,对吗?这意味着我需要给它键和值,但是使用 Array 听起来更简单直接。
标签: javascript qml