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. QTreeWidget select all children of a selected item
Qt 6.11 is out! See what's new in the release blog

QTreeWidget select all children of a selected item

Scheduled Pinned Locked Moved Unsolved Qt for Python
9 Posts 5 Posters 7.8k 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.
  • Z Offline
    Z Offline
    Zweat
    wrote on last edited by
    #1

    Hello,
    I have following tree:

    -Parent1
    ................|_Child 1
    ................|_Child 2
    ................................|_Child of Child 2

    ExtendedSelection is enabled. Now I select Parent1 and click a Button and I want Child1, Child2 and Child of Child 2 to be selected to retrieve data from selectedItems()

    To do something like:

    selectedones = self.tw.selectedItems()
    for item in selectedones:
    print(item.text(2))

    Hope you can understand and help me.
    Kind regards,
    Zweat

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Recursively go through all the item children and call setSelected on them.

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

      1 Reply Last reply
      1
      • Z Offline
        Z Offline
        Zweat
        wrote on last edited by
        #3

        Thanks for welcoming me and the answer. Due to the fact I am pretty new to programming I am questioning myself how I could go through the item children. I tried to read through the Qtreewidget manual but I couldn't get to a conclusion. Therefore, maybe you could provide me a small codesnippet?
        Kind regards,
        Zweat

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          That's because you are looking at the wrong class.

          QTreeWidgetItem::childCount and QTreeWidgetItem::child are what you are looking for.

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

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            Zweat
            wrote on last edited by Zweat
            #5

            Hi,
            now I got I was realy looking for the wrong class.
            Following is what I am doing to select the Items, as the Items are from a SQL Database, I don't know, how many children a child of a child etc. can have. Is there a better way, than write 20 for loops to get deeper?

            
                        for item in selectedones:
                            for i in range(0,item.childCount()):
                                item.child(i).setSelected(True)
                                for n in range(0,item.child(i).childCount()):
                                    item.child(i).child(n).setSelected(True)
                                    for o in range(0,item.child(i).child(n).childCount()):
                                        item.child(i).child(n).child(o).setSelected(True)
            

            Kind regards and thanks for advice!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Use a recursive function

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

              JoeCFDJ 1 Reply Last reply
              1
              • SGaistS SGaist

                Use a recursive function

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

                @SGaist Use iterative loops if possible. Recursion may cause stack overflow.

                JonBJ eyllanescE 2 Replies Last reply
                0
                • JoeCFDJ JoeCFD

                  @SGaist Use iterative loops if possible. Recursion may cause stack overflow.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @JoeCFD said in QTreeWidget select all children of a selected item:

                  @SGaist Use iterative loops if possible. Recursion may cause stack overflow.

                  Joe, my friend, that is not the right approach! It's a very broad statement. Recursing in order to visit this tree to any likely depth is the correct algorithm to use. It will not go anywhere near overflowing the stack for any imaginable depth. And further the recursive function will be incredibly light on local, stack variable sizes; it is when one codes to occupy, say, megabytes of stack space with local variables on each recursive function call that one would have to beware of stack overflow, and that is not the case in Qt-type programming.

                  The OP should be encouraged to use a simple recursive function here (which I was going to say myself yesterday, but didn't bother) as the correct approach, not forced to worry about an unnecessary iterative replacement.

                  1 Reply Last reply
                  1
                  • JoeCFDJ JoeCFD

                    @SGaist Use iterative loops if possible. Recursion may cause stack overflow.

                    eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on last edited by
                    #9

                    @JoeCFD try this:

                    def selected_recursive(item):
                        for row in item.childCount():
                            child_item = item.child(row)
                            child_item.setSelected(True)
                            selected_recursive(child_item)
                    

                    Then:

                    for item in selectedones:
                        selected_recursive(item)
                    

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    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