Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to find element in QTreewidget in python?

How to find element in QTreewidget in python?

Scheduled Pinned Locked Moved Solved Qt for Python
11 Posts 3 Posters 1.4k 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 19 Jan 2024, 20:48 last edited by
    #2

    Hi,

    Do you mean programmatically ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    M 1 Reply Last reply 20 Jan 2024, 04:57
    0
    • S SGaist
      19 Jan 2024, 20:48

      Hi,

      Do you mean programmatically ?

      M Offline
      M Offline
      MarcinOS
      wrote on 20 Jan 2024, 04:57 last edited by MarcinOS
      #3

      @SGaist Of course :D
      Fortunately, I don't have a problem selecting it with the mouse ;-)

      J 1 Reply Last reply 20 Jan 2024, 13:24
      0
      • M MarcinOS
        20 Jan 2024, 04:57

        @SGaist Of course :D
        Fortunately, I don't have a problem selecting it with the mouse ;-)

        J Offline
        J Offline
        JonB
        wrote on 20 Jan 2024, 13:24 last edited by
        #4

        @MarcinOS
        Well, you just parse that into separate segments by QString::split('/') and then walk down the tree (from the top, you can do it either recursively or iteratively) matching each element against a child. What you look at depends on what you are matching each item against, e.g. perhaps it's the model's DataRole value. And obviously depending on what you want you can either select just the bottom element or all the parent nodes leading to it as well as you go.

        1 Reply Last reply
        1
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 20 Jan 2024, 15:51 last edited by
          #5

          In addition to what @JonB wrote, you are looking for the match model function.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply 20 Jan 2024, 16:03
          1
          • S SGaist
            20 Jan 2024, 15:51

            In addition to what @JonB wrote, you are looking for the match model function.

            J Offline
            J Offline
            JonB
            wrote on 20 Jan 2024, 16:03 last edited by JonB
            #6

            @SGaist
            If I may, at the risk of being shot down, this is not the right call to use here! Two reasons:

            • Perform a search on all items looking at their value. Say there are a million nodes/leaves in the model! For parent/parent/parent/parent/element OP knows where he wants to go to, search every value and returning a list is not efficient.

            • Further, for parent/parent/parent/parent/element searching for element does not tell you whether/which one found has desired parent/parent/parent/parent path. You are going to have to look back up ancestors of found items to discover this. Wasted effort.

            Assuming OP does mean by parent/parent/parent/parent/element that the search is intended to pick the particular parents specified. Different if he does not actually care what those are, but that's not what the question implies (to me).

            S 1 Reply Last reply 20 Jan 2024, 21:53
            0
            • M Offline
              M Offline
              MarcinOS
              wrote on 20 Jan 2024, 17:30 last edited by
              #7

              Can I ask for a piece of code? I'm a very new coder, and even though I've used many PyQT elements in my application, QTreewidget is too complicated for me. I managed to fill it with data and retrieve the entire path to the selected item correctly, but searching for and selecting an item in the tree based on its path is beyond my current abilities.

              J 1 Reply Last reply 20 Jan 2024, 20:58
              0
              • M MarcinOS
                20 Jan 2024, 17:30

                Can I ask for a piece of code? I'm a very new coder, and even though I've used many PyQT elements in my application, QTreewidget is too complicated for me. I managed to fill it with data and retrieve the entire path to the selected item correctly, but searching for and selecting an item in the tree based on its path is beyond my current abilities.

                J Offline
                J Offline
                JonB
                wrote on 20 Jan 2024, 20:58 last edited by
                #8

                @MarcinOS said in How to find element in QTreewidget in python?:

                retrieve the entire path to the selected item correctly,

                What do you mean by this? I didn't think QTreeWidget has an actual "path", what code did you use? Are you talking about writing your code like, say, https://stackoverflow.com/a/41042103/489865 (very first sample code box)?

                1 Reply Last reply
                0
                • J JonB
                  20 Jan 2024, 16:03

                  @SGaist
                  If I may, at the risk of being shot down, this is not the right call to use here! Two reasons:

                  • Perform a search on all items looking at their value. Say there are a million nodes/leaves in the model! For parent/parent/parent/parent/element OP knows where he wants to go to, search every value and returning a list is not efficient.

                  • Further, for parent/parent/parent/parent/element searching for element does not tell you whether/which one found has desired parent/parent/parent/parent path. You are going to have to look back up ancestors of found items to discover this. Wasted effort.

                  Assuming OP does mean by parent/parent/parent/parent/element that the search is intended to pick the particular parents specified. Different if he does not actually care what those are, but that's not what the question implies (to me).

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 20 Jan 2024, 21:53 last edited by
                  #9

                  @JonB said in How to find element in QTreewidget in python?:

                  @SGaist
                  If I may, at the risk of being shot down, this is not the right call to use here! Two reasons:

                  • Perform a search on all items looking at their value. Say there are a million nodes/leaves in the model! For parent/parent/parent/parent/element OP knows where he wants to go to, search every value and returning a list is not efficient.

                  • Further, for parent/parent/parent/parent/element searching for element does not tell you whether/which one found has desired parent/parent/parent/parent path. You are going to have to look back up ancestors of found items to discover this. Wasted effort.

                  Assuming OP does mean by parent/parent/parent/parent/element that the search is intended to pick the particular parents specified. Different if he does not actually care what those are, but that's not what the question implies (to me).

                  No reason to shoot you down. My idea was rather to use match following each level so you should only do the search on the elements from the child column of the previous item.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply 20 Jan 2024, 22:19
                  0
                  • S SGaist
                    20 Jan 2024, 21:53

                    @JonB said in How to find element in QTreewidget in python?:

                    @SGaist
                    If I may, at the risk of being shot down, this is not the right call to use here! Two reasons:

                    • Perform a search on all items looking at their value. Say there are a million nodes/leaves in the model! For parent/parent/parent/parent/element OP knows where he wants to go to, search every value and returning a list is not efficient.

                    • Further, for parent/parent/parent/parent/element searching for element does not tell you whether/which one found has desired parent/parent/parent/parent path. You are going to have to look back up ancestors of found items to discover this. Wasted effort.

                    Assuming OP does mean by parent/parent/parent/parent/element that the search is intended to pick the particular parents specified. Different if he does not actually care what those are, but that's not what the question implies (to me).

                    No reason to shoot you down. My idea was rather to use match following each level so you should only do the search on the elements from the child column of the previous item.

                    J Offline
                    J Offline
                    JonB
                    wrote on 20 Jan 2024, 22:19 last edited by
                    #10

                    @SGaist said in How to find element in QTreewidget in python?:

                    My idea was rather to use match following each level so you should only do the search on the elements from the child column of the previous item.

                    I was thinking/assuming recursive. I forgot match() can do one level only, that changes things and makes it fine! :)

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MarcinOS
                      wrote on 22 Jan 2024, 08:44 last edited by
                      #11

                      Problem solved :)
                      https://stackoverflow.com/q/77850224/21149846

                      1 Reply Last reply
                      0
                      • M MarcinOS has marked this topic as solved on 22 Jan 2024, 08:45

                      11/11

                      22 Jan 2024, 08:44

                      • 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