在 ListModel 中传递数组

Bea*_*ear 2 javascript qml

我想知道如何在 ListModel 中传递数组?

好的,在 QML 中,我有一个 ListView,我将其设置ListModel如下:

model: ListModel
{
    id: myList   
    ListElement 
    {
        name: ""
        card: 0
        books: []
    }

}
Run Code Online (Sandbox Code Playgroud)

我可以使用以下方法附加到它:

myList.append({name:"terry", card:00100, books:["024589","865976","879582","215645"]}); 
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在屏幕上输出它时,我得到了这个。

{
    "card": 00100
    "books": {
        "objectName": "",
        "count": 4,
        "dynamicRoles": false
     },
    "name": "terry",
    "name": "terry"
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我会得到 2 个名字!以及如何获得书籍的价值?

我仰望的QML文件的ListModelListElement找不到,所有的例子是整数或字符串与传递一个数组东西。

知道如何获得日期吗?
我确实通过在Delegate 中调用数组来解决它,Component.onCompleted:{}但我认为这不是一个好的/正确的方法,因为Delegate不负责保存数据并且应该在Model 中完成,如果我错了,请纠正我。

谢谢你的时间。

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
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在将名称提供Label ( id: nameID ),并且我想将书籍提供给具有模型的ComboBox ( id: booksID ) ,如果我将书籍的键设为ListElement我该如何提供所有值?

在 QML ListModelListElement文档中没有提到有关获取所有键值的任何内容吗?它只支持get(int index)基于索引号的。

S.M*_*avi 7

你做错了。数组成员必须是ListElement

ListModel {
    id: mod
    ListElement {
        name: "ali"
        dic: [ ListElement{text:"asad-o-llah"; code: 14}, ListElement{text:"aboo torab"; code: 72}, ListElement{text:"amir al-momenin"; code: 110}]
    }
}

ListView {
    model: mod
    anchors.fill: parent
    delegate: Component {
        Rectangle {
            width: parent.width; height: 50
            Row {
                Text {
                    text: name
                }
                ComboBox {
                    width: 100; height: 30
                    model: dic        //<-- set dic as model for combo box
                    textRole: "text"  //<-- important!
                    onCurrentIndexChanged: {
                        console.log("current code is "+model.get(currentIndex).code);   //<-- get code value
                    }
                }
            }
        }
    }
}
Component.onCompleted: {
    var v = mod.get(0).dic.get(0).value;    //<-- sample usage
    console.log(v);
}
Run Code Online (Sandbox Code Playgroud)

  • 我更新了我的答案以解释如何在组合框中使用子模型。 (2认同)