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. How to retrieve items indices from a treeView?
Forum Updated to NodeBB v4.3 + New Features

How to retrieve items indices from a treeView?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmltreeviewmodel-viewdelegatesindex
11 Posts 3 Posters 7.7k Views 1 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #2

    Just add the third parameter to the index method:

    theModel.index(0,1) retrieves row 0, column 1 of the root
    theModel.index(0,1,theModel.index(0,0)) retrieves row 0, column 1 of the child of the first item in the root

    I hope this is clear enough

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    1
    • K Offline
      K Offline
      Kofr
      wrote on last edited by
      #3

      Be aware that TreeView is very limited and has many bugs.
      Indexes are unsafe to use. You can find bugreports on qt bugreports.
      You may want to support https://bugreports.qt.io/browse/QTBUG-56490?filter=-2

      VRoninV HaithamH 2 Replies Last reply
      1
      • K Kofr

        Be aware that TreeView is very limited and has many bugs.
        Indexes are unsafe to use. You can find bugreports on qt bugreports.
        You may want to support https://bugreports.qt.io/browse/QTBUG-56490?filter=-2

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #4

        @Kofr said in How to retrieve items indices from a treeView?:

        Indexes are unsafe to use

        Just to clarify: They are 100% safe to use in the C++ side

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        K HaithamH 2 Replies Last reply
        0
        • VRoninV VRonin

          @Kofr said in How to retrieve items indices from a treeView?:

          Indexes are unsafe to use

          Just to clarify: They are 100% safe to use in the C++ side

          K Offline
          K Offline
          Kofr
          wrote on last edited by
          #5

          @VRonin so far they are not manipulated by TreeView itself

          1 Reply Last reply
          0
          • K Kofr

            Be aware that TreeView is very limited and has many bugs.
            Indexes are unsafe to use. You can find bugreports on qt bugreports.
            You may want to support https://bugreports.qt.io/browse/QTBUG-56490?filter=-2

            HaithamH Offline
            HaithamH Offline
            Haitham
            wrote on last edited by
            #6

            @Kofr I've read your question one year ago, referring to this one

            That's why you are saying it has many bugs?

            let me clarify why I needed to retrieve the index, I'm trying to access each item on its own so I can apply a specific delegate to it (i.e. the green icon next to Camera connected will turn on if a specific condition is met on the back-end, that's why I tried to address each item on its own.

            So far the method VRonin provided worked well and gave me the data I expected, so I am hoping when I finish my code, manipulating the items through the back-end, the delegate will be applied correctly.

            I will keep you posted if I encountered any problem, thanks for the heads up.

            K 1 Reply Last reply
            0
            • HaithamH Haitham

              @Kofr I've read your question one year ago, referring to this one

              That's why you are saying it has many bugs?

              let me clarify why I needed to retrieve the index, I'm trying to access each item on its own so I can apply a specific delegate to it (i.e. the green icon next to Camera connected will turn on if a specific condition is met on the back-end, that's why I tried to address each item on its own.

              So far the method VRonin provided worked well and gave me the data I expected, so I am hoping when I finish my code, manipulating the items through the back-end, the delegate will be applied correctly.

              I will keep you posted if I encountered any problem, thanks for the heads up.

              K Offline
              K Offline
              Kofr
              wrote on last edited by
              #7

              @Haitham the problems begin occures when you make item moves in TreeView. After this operation items are shown incorrectly and you can not catch correct indexes unfortunataly.

              HaithamH 1 Reply Last reply
              1
              • VRoninV VRonin

                @Kofr said in How to retrieve items indices from a treeView?:

                Indexes are unsafe to use

                Just to clarify: They are 100% safe to use in the C++ side

                HaithamH Offline
                HaithamH Offline
                Haitham
                wrote on last edited by
                #8

                @VRonin
                Thanks so much buddy, your method worked and I understood it, the problem with my method that I wasn't referring to a parent item so I can get its child.

                What I eventually want to achieve is to let the delegates react to some back-end code, so I want to apply delegates to only a specific item once the condition is met (e.g., if Camera is connected, the green off icon should be replaced with a green on one) and so on. By saying so, I think it makes sense now why I wanted to be able to reach each item through its index.

                Please, let me follow it by another question,
                is it better to address the items on the C++ side or do it on the QML side?

                VRoninV 1 Reply Last reply
                0
                • K Kofr

                  @Haitham the problems begin occures when you make item moves in TreeView. After this operation items are shown incorrectly and you can not catch correct indexes unfortunataly.

                  HaithamH Offline
                  HaithamH Offline
                  Haitham
                  wrote on last edited by
                  #9

                  @Kofr

                  So far in my case, the items are all static and only the delegates are updated. I think I should not encounter this problem but we will see

                  Thanks a lot, once again!

                  1 Reply Last reply
                  1
                  • HaithamH Haitham

                    @VRonin
                    Thanks so much buddy, your method worked and I understood it, the problem with my method that I wasn't referring to a parent item so I can get its child.

                    What I eventually want to achieve is to let the delegates react to some back-end code, so I want to apply delegates to only a specific item once the condition is met (e.g., if Camera is connected, the green off icon should be replaced with a green on one) and so on. By saying so, I think it makes sense now why I wanted to be able to reach each item through its index.

                    Please, let me follow it by another question,
                    is it better to address the items on the C++ side or do it on the QML side?

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #10

                    @Haitham said in How to retrieve items indices from a treeView?:

                    What I eventually want to achieve is to let the delegates react to some back-end code, so I want to apply delegates to only a specific item once the condition is met (e.g., if Camera is connected, the green off icon should be replaced with a green on one) and so on. By saying so, I think it makes sense now why I wanted to be able to reach each item through its index.

                    Just save the state as a separate role in the item and let the delegate check that role before deciding what to do

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    HaithamH 1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @Haitham said in How to retrieve items indices from a treeView?:

                      What I eventually want to achieve is to let the delegates react to some back-end code, so I want to apply delegates to only a specific item once the condition is met (e.g., if Camera is connected, the green off icon should be replaced with a green on one) and so on. By saying so, I think it makes sense now why I wanted to be able to reach each item through its index.

                      Just save the state as a separate role in the item and let the delegate check that role before deciding what to do

                      HaithamH Offline
                      HaithamH Offline
                      Haitham
                      wrote on last edited by Haitham
                      #11

                      @VRonin
                      Can you please provide an example or something to follow? Because I am still a newbie at both Qt and QML.
                      Sorry for bothering you with my many questions.

                      Update:
                      as you can see in the code, I change the state of the delegate through the mouse area in the delegate (it's commented out in the code). I was using it to test the states, now I've noticed another thing; Whenever I collapse the parent Item and then expand it, the previous states are not saved....does this have to do anything with what you mentioned?

                      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