Pass an Array in ListModel



  • I'm wondering how can I pass an array in ListModel?

    ok, In QML I have a ListView and I set it's ListModel like so:

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

    I can append to it by using:

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

    but when I try to output it on screen I get this.

    {
        "card": 00100
        "books": {
            "objectName": "",
            "count": 4,
            "dynamicRoles": false
         },
        "name": "terry",
        "name": "terry"
    }
    

    I'm not sure why I'm getting 2 names though! and how can I get the value of books?

    I look up the QML documentation of ListModel and ListElement couldn't find anything related to passing an array, all the examples are integer or string.

    Any idea how I can get the books date?
    I did work around it by calling the array in Delegate with Component.onCompleted:{} but I believe that's not a good/correct way since Delegate is not responsible for holding the data and should be done in Model, please do correct me if I'm wrong.

    Thanks for your time.

    Edit01: here is the reason I need array:
    I have a ComboBox in Delegate like so:

    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
             }
         }
    }
    

    as you can see I'm feeding the name to Label (id: nameID) and I want to feed the books to ComboBox (id: booksID) that has model.

    in QML ListModel or ListElement documentation didn't mention anything about getting all the key's value otherwise I would have used the ListElement, it only supports get(int index) which is based on an index number and I believe If I try to use Component.onCompleted:{} on ComboBox, it would slow down the performance. any suggestion?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.