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. [SOLVED] Create a ListModel with Objects and lists (QML)
Forum Update on Monday, May 27th 2025

[SOLVED] Create a ListModel with Objects and lists (QML)

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 2.1k Views
  • 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.
  • D Offline
    D Offline
    DerMas
    wrote on 23 Oct 2013, 14:02 last edited by
    #1

    I get some data, which I'd like to show in a ListView. With simple strings, it is not a problem, but with complex data types and lists I cant get it to work.

    Here is an example of the data I'd like to display (in json notation):

    @
    {
    "customer": {
    "name": "John",
    "products": [
    {
    "name": "p1",
    "price": 4.96
    },
    {
    "name": "p2",
    "price": 5.82
    }
    ]
    }
    }
    @

    So I have to put into the ListModel the complex type "customer" with a list of the complex type "products". Any ideas how to solve that problem?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gennon
      wrote on 24 Oct 2013, 09:41 last edited by
      #2

      The documentation explains this: http://qt-project.org/doc/qt-5.0/qtqml/qml-qtquick2-listmodel.html#example-usage

      @
      ListModel {
      id: customerModel

      ListElement {
          name: "John"
          products: [
              ListElement { name: "P1";  price:  4.96 },
              ListElement { name: "P2";  price:  5.82 }
          ]
      }    
      

      }
      @

      /Gen

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DerMas
        wrote on 24 Oct 2013, 09:50 last edited by
        #3

        Yap, thats what the docs say :) But as far as I know, this works only for static content. I couldnt figure out, how to add dynamically content.

        For example I tried something like this:
        @
        ListModel{
        id: myListModel
        ListElement{
        customerName = ""
        products = ""
        ]
        }

        function fillList(){
        var listData;
        listData = {"customerName" : "John", products = {"name" : "p1", "price" : "4.96"}};
        myListModel.append(listData);
        }
        @

        But this doesnt work, because the types are not compatible.
        Maybe you could tell how to do it the right way, that would help alot =)

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gennon
          wrote on 24 Oct 2013, 13:28 last edited by
          #4

          This works:

          @ListModel{
          id: myListModel
          ListElement{
          name: "Eddie"
          products: [
          ListElement{ name: "p0"; price: 3.99 },
          ListElement{ name: "p1"; price: 4.96 }
          ]
          }

          }
          
          function fillList(){
           var listData;
              listData = {"name" : "John", "products" : [
                            {"name" : "p1", "price" : 4.96},
                            {"name" : "p2", "price" : 5.98} 
                       ]};
              myListModel.append(listData);
              label.text = myListModel.get(myListModel.count-1).products.get(0).name;
          }@
          

          /Gen

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerMas
            wrote on 24 Oct 2013, 13:40 last edited by
            #5

            Thanks, works great. So I just missed those two brackets, damn it ;)

            For others having the same problem: It also works with an empty initial ListModel, you just have to declare the list attribute as list with empty brackets:

            @
            ListModel{
            id: myListModel
            ListElement{
            name: ""
            products: []
            }
            }
            @

            1 Reply Last reply
            0
            • G Offline
              G Offline
              Gennon
              wrote on 25 Oct 2013, 05:48 last edited by
              #6

              You should even be able to have a complete empty ListModel;

              @
              ListModel{
              id: myListModel
              }@

              /Gen

              1 Reply Last reply
              0

              6/6

              25 Oct 2013, 05:48

              • Login

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