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. QML ListView: model like Array of Arrays. How?
Forum Updated to NodeBB v4.3 + New Features

QML ListView: model like Array of Arrays. How?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 1.0k 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.
  • B Offline
    B Offline
    bogong
    wrote on 24 Jun 2020, 15:14 last edited by bogong
    #1

    Hello all!
    Is there any way to pass array of arrays to QML ListView and how to access data from this array-of-arrays?
    Something like this:

    property var pData: [
    	["Field_0-01","Field_0-02","Field_0-03","Field_0-04","Field_0-05"],
    	["Field_1-01","Field_1-02","Field_1-03","Field_1-04","Field_1-05"],
    	["Field_2-01","Field_2-02","Field_2-03","Field_1-04","Field_2-05"],
    	["Field_3-01","Field_3-02","Field_3-03","Field_1-04","Field_3-05"],
    	["Field_4-01","Field_4-02","Field_4-03","Field_1-04","Field_4-05"],
    	["Field_5-01","Field_5-02","Field_5-03","Field_1-04","Field_5-05"],
    	["Field_6-01","Field_6-02","Field_6-03","Field_1-04","Field_6-05"],
    	["Field_7-01","Field_7-02","Field_7-03","Field_1-04","Field_7-05"]
    ];
    
    ...
    
    ListView {
    
    	...
    	model: pData;
    	...
    }
    

    For me works only direct access to pData by currentIndex of ListView:

    oApplicationWindow.pData[index][0]
    

    But is there way to access it through "model"?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 25 Jun 2020, 07:06 last edited by
      #2

      This might work:

      modelData[0]
      

      (Z(:^

      B 2 Replies Last reply 26 Jun 2020, 05:10
      1
      • S sierdzio
        25 Jun 2020, 07:06

        This might work:

        modelData[0]
        
        B Offline
        B Offline
        bogong
        wrote on 26 Jun 2020, 05:10 last edited by
        #3

        @sierdzio Thx. It works.

        1 Reply Last reply
        0
        • S sierdzio
          25 Jun 2020, 07:06

          This might work:

          modelData[0]
          
          B Offline
          B Offline
          bogong
          wrote on 9 Jul 2020, 07:46 last edited by
          #4

          @sierdzio Do you know which method should be implemented for the same functionality if I am developing my own/custom data model based on QAbstractListModel?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 9 Jul 2020, 07:54 last edited by
            #5

            Declare roles by reimplementing roleNames(), then, in data() use these roles to return your data. In QML, use the names specified by roleNames() to access the data.

            So, for a very short example (pseudocode, sorry I don't have time to check all params etc.):

            QHash<> roleNames() {
              return { Qt::UserRole + 1, "myRole" };
            }
            
            QVariant data() {
              if (role == Qt::UserRole+1) {
                return QVariantList{ "A", "B", "C" };
              }
            }
            
            /// QML:
            console.log(myRole[0] // Will print "A")
            

            (Z(:^

            B 1 Reply Last reply 9 Jul 2020, 08:27
            1
            • S sierdzio
              9 Jul 2020, 07:54

              Declare roles by reimplementing roleNames(), then, in data() use these roles to return your data. In QML, use the names specified by roleNames() to access the data.

              So, for a very short example (pseudocode, sorry I don't have time to check all params etc.):

              QHash<> roleNames() {
                return { Qt::UserRole + 1, "myRole" };
              }
              
              QVariant data() {
                if (role == Qt::UserRole+1) {
                  return QVariantList{ "A", "B", "C" };
                }
              }
              
              /// QML:
              console.log(myRole[0] // Will print "A")
              
              B Offline
              B Offline
              bogong
              wrote on 9 Jul 2020, 08:27 last edited by
              #6

              @sierdzio Thx. I've got roleNames missed. It's mean "modelData" just a role name. I though it's Qt-system value. Briefly all of it looks like model within only one role where stored QVariantList that is transforming into JS-Array in QML.

              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