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

How to retrieve items indices from a treeView?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmltreeviewmodel-viewdelegatesindex
11 Posts 3 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.
  • V Offline
    V Offline
    VRonin
    wrote on 3 Jul 2017, 07:28 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 3 Jul 2017, 08:09 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

      V H 2 Replies Last reply 3 Jul 2017, 09:29
      1
      • K Kofr
        3 Jul 2017, 08:09

        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

        V Offline
        V Offline
        VRonin
        wrote on 3 Jul 2017, 09:29 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 H 2 Replies Last reply 3 Jul 2017, 11:50
        0
        • V VRonin
          3 Jul 2017, 09:29

          @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 3 Jul 2017, 11:50 last edited by
          #5

          @VRonin so far they are not manipulated by TreeView itself

          1 Reply Last reply
          0
          • K Kofr
            3 Jul 2017, 08:09

            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

            H Offline
            H Offline
            Haitham
            wrote on 3 Jul 2017, 12:19 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 3 Jul 2017, 12:25
            0
            • H Haitham
              3 Jul 2017, 12:19

              @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 3 Jul 2017, 12:25 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.

              H 1 Reply Last reply 3 Jul 2017, 12:32
              1
              • V VRonin
                3 Jul 2017, 09:29

                @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

                H Offline
                H Offline
                Haitham
                wrote on 3 Jul 2017, 12:25 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?

                V 1 Reply Last reply 3 Jul 2017, 12:35
                0
                • K Kofr
                  3 Jul 2017, 12:25

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

                  H Offline
                  H Offline
                  Haitham
                  wrote on 3 Jul 2017, 12:32 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
                  • H Haitham
                    3 Jul 2017, 12:25

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

                    V Offline
                    V Offline
                    VRonin
                    wrote on 3 Jul 2017, 12:35 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

                    H 1 Reply Last reply 3 Jul 2017, 13:18
                    0
                    • V VRonin
                      3 Jul 2017, 12:35

                      @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

                      H Offline
                      H Offline
                      Haitham
                      wrote on 3 Jul 2017, 13:18 last edited by Haitham 7 Mar 2017, 23:36
                      #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

                      11/11

                      3 Jul 2017, 13:18

                      • Login

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