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. Get QModelIndex from Treeview rowDelegate
Forum Update on Monday, May 27th 2025

Get QModelIndex from Treeview rowDelegate

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
19 Posts 2 Posters 7.7k 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.
  • I Offline
    I Offline
    illunara
    wrote on 12 Nov 2015, 21:16 last edited by
    #1

    Hi everybody
    The rowDelegate of treeview only allow me to access styleData.row, which is not enough since i need to draw row base on item's other attribute too

    So, by any chance, can i get it? Thanks :)

    P 1 Reply Last reply 13 Nov 2015, 07:00
    0
    • I illunara
      12 Nov 2015, 21:16

      Hi everybody
      The rowDelegate of treeview only allow me to access styleData.row, which is not enough since i need to draw row base on item's other attribute too

      So, by any chance, can i get it? Thanks :)

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 13 Nov 2015, 07:00 last edited by
      #2

      @illunara You can call index directly using the model's instance and you already have row and column to pass it to. Eg. here.

      157

      I 1 Reply Last reply 13 Nov 2015, 08:32
      0
      • P p3c0
        13 Nov 2015, 07:00

        @illunara You can call index directly using the model's instance and you already have row and column to pass it to. Eg. here.

        I Offline
        I Offline
        illunara
        wrote on 13 Nov 2015, 08:32 last edited by
        #3

        @p3c0 Hi p3c0, it's good to see you again :)

        I tried the index(row, column, parentIndex) already. Indeed ,it returns a QModelIndex of the first level item, but not working with child item. We already talked about this on other topic, without QModelIndex, we can't get the item from treeview.

        I also read the post you recommend, but still not finger out how to do it.Am i missing something?
        Thanks

        P 1 Reply Last reply 14 Nov 2015, 06:31
        0
        • I illunara
          13 Nov 2015, 08:32

          @p3c0 Hi p3c0, it's good to see you again :)

          I tried the index(row, column, parentIndex) already. Indeed ,it returns a QModelIndex of the first level item, but not working with child item. We already talked about this on other topic, without QModelIndex, we can't get the item from treeview.

          I also read the post you recommend, but still not finger out how to do it.Am i missing something?
          Thanks

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 14 Nov 2015, 06:31 last edited by
          #4

          @illunara You can make use of a little bit C++ side code in this case. Create a Q_INVOKABLE function which will accept the row. Then you can get the children of that particular top level item and then return their index's.

          157

          I 1 Reply Last reply 14 Nov 2015, 06:55
          0
          • P p3c0
            14 Nov 2015, 06:31

            @illunara You can make use of a little bit C++ side code in this case. Create a Q_INVOKABLE function which will accept the row. Then you can get the children of that particular top level item and then return their index's.

            I Offline
            I Offline
            illunara
            wrote on 14 Nov 2015, 06:55 last edited by
            #5

            @p3c0 Yeah, that's the problem. how can i get the parent level item?
            With tableview or listview, this can get done easily since it has only one level, treeview is difference :(

            P 1 Reply Last reply 14 Nov 2015, 06:57
            0
            • I illunara
              14 Nov 2015, 06:55

              @p3c0 Yeah, that's the problem. how can i get the parent level item?
              With tableview or listview, this can get done easily since it has only one level, treeview is difference :(

              P Offline
              P Offline
              p3c0
              Moderators
              wrote on 14 Nov 2015, 06:57 last edited by
              #6

              @illunara styleData.row gives you the row. Using it get the model index for top level item.

              157

              I 1 Reply Last reply 14 Nov 2015, 08:15
              0
              • P p3c0
                14 Nov 2015, 06:57

                @illunara styleData.row gives you the row. Using it get the model index for top level item.

                I Offline
                I Offline
                illunara
                wrote on 14 Nov 2015, 08:15 last edited by
                #7

                @p3c0 But if the item is not child of top, but sub-level. I will get an error O_O

                P 1 Reply Last reply 14 Nov 2015, 08:35
                0
                • I illunara
                  14 Nov 2015, 08:15

                  @p3c0 But if the item is not child of top, but sub-level. I will get an error O_O

                  P Offline
                  P Offline
                  p3c0
                  Moderators
                  wrote on 14 Nov 2015, 08:35 last edited by
                  #8

                  @illunara The only way is to iterate and find children of the top level item and then children of these children and so on.

                  157

                  I 1 Reply Last reply 14 Nov 2015, 10:12
                  0
                  • P p3c0
                    14 Nov 2015, 08:35

                    @illunara The only way is to iterate and find children of the top level item and then children of these children and so on.

                    I Offline
                    I Offline
                    illunara
                    wrote on 14 Nov 2015, 10:12 last edited by
                    #9

                    @p3c0 Can we access to the current component of rowDelegate?
                    Like that, we can create a work-around using itemDelegate?

                    P 1 Reply Last reply 16 Nov 2015, 07:31
                    0
                    • I illunara
                      14 Nov 2015, 10:12

                      @p3c0 Can we access to the current component of rowDelegate?
                      Like that, we can create a work-around using itemDelegate?

                      P Offline
                      P Offline
                      p3c0
                      Moderators
                      wrote on 16 Nov 2015, 07:31 last edited by
                      #10

                      @illunara Afaik getting data through model instead of delegate is the recommended way. Didn't it work through model ?

                      157

                      I 1 Reply Last reply 16 Nov 2015, 09:39
                      0
                      • P p3c0
                        16 Nov 2015, 07:31

                        @illunara Afaik getting data through model instead of delegate is the recommended way. Didn't it work through model ?

                        I Offline
                        I Offline
                        illunara
                        wrote on 16 Nov 2015, 09:39 last edited by
                        #11

                        @p3c0 The only thing i have frow rowDelegate is row number, i can't get model data just with that (Except on the top level)

                        P 1 Reply Last reply 16 Nov 2015, 09:52
                        0
                        • I illunara
                          16 Nov 2015, 09:39

                          @p3c0 The only thing i have frow rowDelegate is row number, i can't get model data just with that (Except on the top level)

                          P Offline
                          P Offline
                          p3c0
                          Moderators
                          wrote on 16 Nov 2015, 09:52 last edited by
                          #12

                          @illunara Why not ? For eg. following code gives you QModelIndex's of all top level item.

                          var top = [];
                          for (var i = 0; i < myModel.rowCount(); ++i ) {
                              top[i] = myModel.index( i, 0 );
                          }
                          console.log(top)
                          

                          Once you get the top level you can get their children. It would be better if you do the same on the C++ side as I think getting children should be lot more easier there.

                          157

                          I 1 Reply Last reply 16 Nov 2015, 12:04
                          0
                          • P p3c0
                            16 Nov 2015, 09:52

                            @illunara Why not ? For eg. following code gives you QModelIndex's of all top level item.

                            var top = [];
                            for (var i = 0; i < myModel.rowCount(); ++i ) {
                                top[i] = myModel.index( i, 0 );
                            }
                            console.log(top)
                            

                            Once you get the top level you can get their children. It would be better if you do the same on the C++ side as I think getting children should be lot more easier there.

                            I Offline
                            I Offline
                            illunara
                            wrote on 16 Nov 2015, 12:04 last edited by
                            #13

                            @p3c0 Well, because i want to draw the row's color base on item data's attribute. Say :
                            I have a field in the item which tell me if data had been changed or not, if yes, then set the row's color to blue. But i can't access to its data with only row number of rowDelegate

                            If i have QModelIndex i can do it normally with

                               static_cast<Class*>(index.internalPointer()).getData(....)
                            

                            Here my my rowDelegate code for TableView btw
                            ...

                             rowDelegate : Rectangle {
                                color : (model.get(styleData.row, "DataChanged")  ? "#000000" : "#ffffff"
                            }
                            

                            ...

                            P 1 Reply Last reply 18 Nov 2015, 05:16
                            0
                            • I illunara
                              16 Nov 2015, 12:04

                              @p3c0 Well, because i want to draw the row's color base on item data's attribute. Say :
                              I have a field in the item which tell me if data had been changed or not, if yes, then set the row's color to blue. But i can't access to its data with only row number of rowDelegate

                              If i have QModelIndex i can do it normally with

                                 static_cast<Class*>(index.internalPointer()).getData(....)
                              

                              Here my my rowDelegate code for TableView btw
                              ...

                               rowDelegate : Rectangle {
                                  color : (model.get(styleData.row, "DataChanged")  ? "#000000" : "#ffffff"
                              }
                              

                              ...

                              P Offline
                              P Offline
                              p3c0
                              Moderators
                              wrote on 18 Nov 2015, 05:16 last edited by
                              #14

                              @illunara Try making use of dataChanged signal. I hope you do that in model when you update the data. Then on QML side you can use Connections to listen to the signal keeping the model as target. When the signal is emitted the corresponding handler will be invoked which will return the model indexes which were updated. Once you get those indexes getting its value(data) should be easier IMO. I haven't tried it myself but seems workable.

                              157

                              I 1 Reply Last reply 18 Nov 2015, 07:26
                              0
                              • P p3c0
                                18 Nov 2015, 05:16

                                @illunara Try making use of dataChanged signal. I hope you do that in model when you update the data. Then on QML side you can use Connections to listen to the signal keeping the model as target. When the signal is emitted the corresponding handler will be invoked which will return the model indexes which were updated. Once you get those indexes getting its value(data) should be easier IMO. I haven't tried it myself but seems workable.

                                I Offline
                                I Offline
                                illunara
                                wrote on 18 Nov 2015, 07:26 last edited by
                                #15

                                @p3c0 Still, without access to the rowDelegate component, even if i have modelIndex of the item, nothing can be done.

                                And i think the rowDelegate invoke got invoked before emit dataChanged signal.
                                Maybe we should left it at here, i will think another way to handle this.
                                Thank you so much for helping me :)

                                P 1 Reply Last reply 18 Nov 2015, 07:35
                                0
                                • I illunara
                                  18 Nov 2015, 07:26

                                  @p3c0 Still, without access to the rowDelegate component, even if i have modelIndex of the item, nothing can be done.

                                  And i think the rowDelegate invoke got invoked before emit dataChanged signal.
                                  Maybe we should left it at here, i will think another way to handle this.
                                  Thank you so much for helping me :)

                                  P Offline
                                  P Offline
                                  p3c0
                                  Moderators
                                  wrote on 18 Nov 2015, 07:35 last edited by p3c0
                                  #16

                                  @illunara I think it can be done. Once you get the model index you just have to request the new data (since binding to rolename doesn't work). Something like this inside rowDelegate :

                                  Connections {
                                      target: dataModel
                                      onDataChanged: {
                                          var index = topLeft
                                          update(index)
                                      }
                                  }
                                  
                                  function update(modelIndex) {
                                      var value = dataModel.data(modelIndex, dataModel.getRoleKey("SomeRole"))
                                      //SomeRole is the role defined model which reflects the data you require. 
                                      color = value
                                  }
                                  

                                  getRoleKey function is explained here.

                                  157

                                  I 1 Reply Last reply 19 Nov 2015, 08:17
                                  0
                                  • P p3c0
                                    18 Nov 2015, 07:35

                                    @illunara I think it can be done. Once you get the model index you just have to request the new data (since binding to rolename doesn't work). Something like this inside rowDelegate :

                                    Connections {
                                        target: dataModel
                                        onDataChanged: {
                                            var index = topLeft
                                            update(index)
                                        }
                                    }
                                    
                                    function update(modelIndex) {
                                        var value = dataModel.data(modelIndex, dataModel.getRoleKey("SomeRole"))
                                        //SomeRole is the role defined model which reflects the data you require. 
                                        color = value
                                    }
                                    

                                    getRoleKey function is explained here.

                                    I Offline
                                    I Offline
                                    illunara
                                    wrote on 19 Nov 2015, 08:17 last edited by
                                    #17

                                    @p3c0 Nice idea, but this approach have one problem
                                    Whenever a dataChanged emit, every row will get updated. Which mean, we need data to compare and find out which row, that data belong to.

                                    Only thing we have is styleData.row and its not enough for a complicate model. And unfortunately, we can't access to it when call the update() method either. The value is undefined (ofc, I put it inside rowDelegate)

                                    P 1 Reply Last reply 19 Nov 2015, 09:17
                                    0
                                    • I illunara
                                      19 Nov 2015, 08:17

                                      @p3c0 Nice idea, but this approach have one problem
                                      Whenever a dataChanged emit, every row will get updated. Which mean, we need data to compare and find out which row, that data belong to.

                                      Only thing we have is styleData.row and its not enough for a complicate model. And unfortunately, we can't access to it when call the update() method either. The value is undefined (ofc, I put it inside rowDelegate)

                                      P Offline
                                      P Offline
                                      p3c0
                                      Moderators
                                      wrote on 19 Nov 2015, 09:17 last edited by
                                      #18

                                      @illunara

                                      Whenever a dataChanged emit, every row will get updated. Which mean, we need data to compare and find out which row, that data belong to.

                                      Yes. You just need to add a simple condition before calling function update

                                      var index = topLeft
                                      if(index.row===styleData.row)
                                         update(index)
                                      

                                      157

                                      I 1 Reply Last reply 21 Nov 2015, 10:19
                                      0
                                      • P p3c0
                                        19 Nov 2015, 09:17

                                        @illunara

                                        Whenever a dataChanged emit, every row will get updated. Which mean, we need data to compare and find out which row, that data belong to.

                                        Yes. You just need to add a simple condition before calling function update

                                        var index = topLeft
                                        if(index.row===styleData.row)
                                           update(index)
                                        
                                        I Offline
                                        I Offline
                                        illunara
                                        wrote on 21 Nov 2015, 10:19 last edited by illunara
                                        #19

                                        @p3c0
                                        Root
                                        | ---- Item A // Row 1
                                        |#####|---------- Item B // Row2 (Expected Row 1)
                                        |#####|---------- Item C // Row3 (Expected Row 2)
                                        | ---- Item D //Row4 (Exoected Row 2)

                                        styleData.row is relative only to rootItem. That's why this method will not work

                                        1 Reply Last reply
                                        0

                                        1/19

                                        12 Nov 2015, 21:16

                                        • Login

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