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] How do I get role names and count from ListElements inside a ListModel

[SOLVED] How do I get role names and count from ListElements inside a ListModel

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.5k 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.
  • R Offline
    R Offline
    rmdev
    wrote on last edited by
    #1

    Hi everyone,

    I'm trying to get some information about a ListModel and its ListElements, when it's dynamically created and I don't really know the contents.

    Let's use the example from the ListElement documentation:
    @ListModel {
    id: fruitModel

     ListElement {
         name: "Apple"
         cost: 2.45
     }
     ListElement {
         name: "Orange"
         cost: 3.25
     }
     ListElement {
         name: "Banana"
         cost: 1.95
     }
    

    }@

    Now I want to access several things:
    How many elements are in the list model

    • I know I can do this with fruitModel.count. So that's solved.

    Get each individual list element

    • Using a for loop with the count information and fruitModel.get(i) solves this.

    But now let's assume, I don't know what's inside of the list elements and that's where I'm stuck. I want to get the count of the elements inside of the list element and their role names, so I can access their values.

    Using the above example I want to get count = 2 and role names = name and cost

    Is there some way to achieve this? Doesn't matter if it's inside QML or C++

    Thanks in advance!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gennon
      wrote on last edited by
      #2

      I do not know of any way to do that with ListModel and ListElement. So if you really need this method then you should make your own model based on "QAbstractListModel":http://qt-project.org/doc/qt-5/qabstractlistmodel.html.

      /Gen

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        Hi,

        You can get the roles from C++, since ListModel is of type QAbstractItemModel you can find it accordingly using the objectName property.
        Going thru your above example
        @
        ListModel {
        id: fruitModel
        objectName: "mymodel"
        ListElement {
        name: "Apple"
        cost: 2.45
        }
        ListElement {
        name: "Orange"
        cost: 3.25
        }
        ListElement {
        name: "Banana"
        cost: 1.95
        }
        }
        @

        Then from C++, assuming you are using QQuickView
        @
        QQuickView view;
        QAbstractItemModel item = view.rootObject()->findChild<QAbstractItemModel>("mymodel");
        qDebug() << item->roleNames().value(0); //gives the role at 0
        @

        Since "roleNames":http://qt-project.org/doc/qt-5/qabstractitemmodel.html#roleNames returns a Hash key-value pair, you can iterate it and get the roles, count etc..

        Is this what you require ?

        157

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rmdev
          wrote on last edited by
          #4

          Looks great, thanks!

          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