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. Retrieve number of elements of a QAbstractListModel and access items by index and role in qml
Forum Updated to NodeBB v4.3 + New Features

Retrieve number of elements of a QAbstractListModel and access items by index and role in qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
16 Posts 3 Posters 4.4k Views 2 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by maxwell31
    #1

    Hi,

    If I expose a QAbstractListModel to qml, is there a way I can know the number of elements in the model? Or do I need to make my own Q_INVOKABLE function which gives me the number?

    Gojir4G 1 Reply Last reply
    0
    • M maxwell31

      Hi,

      If I expose a QAbstractListModel to qml, is there a way I can know the number of elements in the model? Or do I need to make my own Q_INVOKABLE function which gives me the number?

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @maxwell31 Hi,

      I guess you can use QAbstractItemModel::rowCount.
      Most of QAbstractItemModel's methods can be invoked from QML now.
      As the note says:

      Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by maxwell31
        #3

        Thanks! I did not know I can simply call them from qml.

        Another question: How can I access an item in the model via its role number? would itemData(index(row,0)) work or is there a simpler solution?

        Gojir4G 1 Reply Last reply
        0
        • M maxwell31

          Thanks! I did not know I can simply call them from qml.

          Another question: How can I access an item in the model via its role number? would itemData(index(row,0)) work or is there a simpler solution?

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @maxwell31 you can use data and index

          I guess something like that :

          myModel.data(myModel.index(row, 0), Qt.DispalyRole) 
          
          1 Reply Last reply
          2
          • M Offline
            M Offline
            maxwell31
            wrote on last edited by
            #5

            Thanks again. The way with calling it with data and index is working. I am wondering though whether there is an easier way, which would use the roleNames in my model. Otherwise, I need to register my model class, so that I have the roleEnums available.

            Gojir4G 2 Replies Last reply
            0
            • M maxwell31

              Thanks again. The way with calling it with data and index is working. I am wondering though whether there is an easier way, which would use the roleNames in my model. Otherwise, I need to register my model class, so that I have the roleEnums available.

              Gojir4G Offline
              Gojir4G Offline
              Gojir4
              wrote on last edited by
              #6

              @maxwell31 Usually you use a model with a listview, a repeater, or any other "item" view. Then you access your roles using names defined in your implementation of QAbstractItemModel::roleNames, not the enum values.

              1 Reply Last reply
              0
              • M maxwell31

                Thanks again. The way with calling it with data and index is working. I am wondering though whether there is an easier way, which would use the roleNames in my model. Otherwise, I need to register my model class, so that I have the roleEnums available.

                Gojir4G Offline
                Gojir4G Offline
                Gojir4
                wrote on last edited by
                #7

                @maxwell31 What's is your goal exactly ?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  maxwell31
                  wrote on last edited by
                  #8

                  I understand what you are saying. My problem is the following:

                  I have some c++ objects, which e.g. have a height property. I want to display these objects in my own qml bar chart. In order to compute the maximum height I iterate over the elements in my model in qml, and would like to access the height property. In a repeater or listmodel, the string role name is only available for the current object right? How could I access the height role inside a loop over my objects?

                  Gojir4G 1 Reply Last reply
                  0
                  • M maxwell31

                    I understand what you are saying. My problem is the following:

                    I have some c++ objects, which e.g. have a height property. I want to display these objects in my own qml bar chart. In order to compute the maximum height I iterate over the elements in my model in qml, and would like to access the height property. In a repeater or listmodel, the string role name is only available for the current object right? How could I access the height role inside a loop over my objects?

                    Gojir4G Offline
                    Gojir4G Offline
                    Gojir4
                    wrote on last edited by
                    #9

                    @maxwell31 You can browse them using a loop from QML. But yes you need to register your type in this case to access the enum values.

                    for(var i = 0; i < model.rowCount(); i++){
                    model.data(model.index(i, 0),
                    }

                    I would suggest that you implement this kind of calculation in the C++ side, and just add a method like ``getMaxHeight()" in your model, with Q_INVOKABLE directive. At my opinion, it is easier to write and more efficient

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      maxwell31
                      wrote on last edited by
                      #10

                      Most likely simply going to do

                      qmlRegisterType<MyModel>("MyModel",1,0,"MyModel");
                      

                      is probably the easiest. Though annoying, as it has to be imported in the qml file. Obviously a maybe better option would be to move more of the logic to c++.

                      But I understand it right, that there is no way in QML to iterate over the elements in a model and use the string role name?

                      Gojir4G 1 Reply Last reply
                      0
                      • M maxwell31

                        Most likely simply going to do

                        qmlRegisterType<MyModel>("MyModel",1,0,"MyModel");
                        

                        is probably the easiest. Though annoying, as it has to be imported in the qml file. Obviously a maybe better option would be to move more of the logic to c++.

                        But I understand it right, that there is no way in QML to iterate over the elements in a model and use the string role name?

                        Gojir4G Offline
                        Gojir4G Offline
                        Gojir4
                        wrote on last edited by
                        #11

                        @maxwell31 said in Retrieve number of elements of a QAbstractListModel and access items by index and role in qml:

                        But I understand it right, that there is no way in QML to iterate over the elements in a model and use the string role name?

                        I would say there is not simple way. But you could retrieve the roleNames() map from QML and then resolve role ID into names.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          maxwell31
                          wrote on last edited by
                          #12

                          I think the roleNames map (QHash) does not convert to a type qml understands.

                          Gojir4G 1 Reply Last reply
                          0
                          • M maxwell31

                            I think the roleNames map (QHash) does not convert to a type qml understands.

                            Gojir4G Offline
                            Gojir4G Offline
                            Gojir4
                            wrote on last edited by Gojir4
                            #13

                            @maxwell31 Yes, as I said, there is no simple way. What about adding a method QString roleName(int role) in the model which return the name of a role.

                            @maxwell31 Yes, as I said, there is no simple way. What about adding a method int nameRole(QString roleName) in the model which return the role according to the name.

                            Anyway what does it changes if you use role or name of role ?

                            1 Reply Last reply
                            0
                            • GrecKoG Offline
                              GrecKoG Offline
                              GrecKo
                              Qt Champions 2018
                              wrote on last edited by
                              #14

                              I've created such class as proof of concept : https://github.com/oKcerG/QmlModelHelper (with the WiP readme : https://gist.github.com/oKcerG/eeea734bdacc51b3ae58650de5f05943)

                              in qml you could then do : yourModel.ModelHelper.roleForName("yourRoleName")

                              1 Reply Last reply
                              1
                              • M Offline
                                M Offline
                                maxwell31
                                wrote on last edited by
                                #15

                                @Gojir4 said in Retrieve number of elements of a QAbstractListModel and access items by index and role in qml:

                                Anyway what does it changes if you use role or name of role ?

                                I usually don't know the integer values of my custom roles, and without registering the model (which is a practicable solution) I don't have access to them. But you are right, adding a nameRole function would work fine.

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  maxwell31
                                  wrote on last edited by
                                  #16

                                  @GrecKo said in Retrieve number of elements of a QAbstractListModel and access items by index and role in qml:

                                  I've created such class as proof of concept

                                  Thanks for sharing

                                  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