Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Pass an Array in ListModel
Qt 6.11 is out! See what's new in the release blog

Pass an Array in ListModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Bernard RouhiB Offline
    Bernard RouhiB Offline
    Bernard Rouhi
    wrote on last edited by Bernard Rouhi
    #1

    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?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kenz Dale
      wrote on last edited by
      #2

      Necroing this thread to link to the answer: https://stackoverflow.com/questions/45156895/pass-an-array-in-listmodel

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved