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
QtWS25 Last Chance

Get QModelIndex from Treeview rowDelegate

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
19 Posts 2 Posters 7.6k 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 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 :)

    p3c0P 1 Reply Last reply
    0
    • I illunara

      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 :)

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on 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
      0
      • p3c0P p3c0

        @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 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

        p3c0P 1 Reply Last reply
        0
        • I illunara

          @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

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on 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
          0
          • p3c0P p3c0

            @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 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 :(

            p3c0P 1 Reply Last reply
            0
            • I illunara

              @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 :(

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on 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
              0
              • p3c0P p3c0

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

                I Offline
                I Offline
                illunara
                wrote on last edited by
                #7

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

                p3c0P 1 Reply Last reply
                0
                • I illunara

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

                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on 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
                  0
                  • p3c0P p3c0

                    @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 last edited by
                    #9

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

                    p3c0P 1 Reply Last reply
                    0
                    • I illunara

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

                      p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on 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
                      0
                      • p3c0P p3c0

                        @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 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)

                        p3c0P 1 Reply Last reply
                        0
                        • I illunara

                          @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)

                          p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on 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
                          0
                          • p3c0P p3c0

                            @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 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"
                            }
                            

                            ...

                            p3c0P 1 Reply Last reply
                            0
                            • I illunara

                              @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"
                              }
                              

                              ...

                              p3c0P Offline
                              p3c0P Offline
                              p3c0
                              Moderators
                              wrote on 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
                              0
                              • p3c0P p3c0

                                @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 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 :)

                                p3c0P 1 Reply Last reply
                                0
                                • I illunara

                                  @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 :)

                                  p3c0P Offline
                                  p3c0P Offline
                                  p3c0
                                  Moderators
                                  wrote on 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
                                  0
                                  • p3c0P p3c0

                                    @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 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)

                                    p3c0P 1 Reply Last reply
                                    0
                                    • I illunara

                                      @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)

                                      p3c0P Offline
                                      p3c0P Offline
                                      p3c0
                                      Moderators
                                      wrote on 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
                                      0
                                      • p3c0P p3c0

                                        @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 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

                                        • Login

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