Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Moving QTreeWidgetItems between two QTreeWidgets

Moving QTreeWidgetItems between two QTreeWidgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
26 Posts 7 Posters 4.9k 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.
  • O Offline
    O Offline
    Ovidiu_GCO
    wrote on last edited by
    #1

    Hello!

    I am trying to move some QTreeWidgetItems between two QTreeWidgets (from "Drivers" to "Participants") using ">>" button, as seen in the image below.

    0_1546729775759_QTreeWidget.PNG

    I already tried to make use of QList<QTreeWidgetItem *> along with a QTreeWidgetItemIterator like this:

    
    		QList<QTreeWidgetItem *> listOfDrivers;
    
    		QTreeWidgetItemIterator it(ui.driversList);
    
    		while (*it) {
    			if ((*it)->isSelected()) {
    				listOfDrivers.append( (*it) );
    			}
    			++it;
    		}
    
    		ui.participantsList->insertTopLevelItems(0, listOfDrivers);
    

    The objects are moved in the QList, but it won't copy them in the QTreeWidget ("Participants").

    Any help is appreciated.
    Thank you!

    mrjjM 1 Reply Last reply
    0
    • O Ovidiu_GCO

      Hello!

      I am trying to move some QTreeWidgetItems between two QTreeWidgets (from "Drivers" to "Participants") using ">>" button, as seen in the image below.

      0_1546729775759_QTreeWidget.PNG

      I already tried to make use of QList<QTreeWidgetItem *> along with a QTreeWidgetItemIterator like this:

      
      		QList<QTreeWidgetItem *> listOfDrivers;
      
      		QTreeWidgetItemIterator it(ui.driversList);
      
      		while (*it) {
      			if ((*it)->isSelected()) {
      				listOfDrivers.append( (*it) );
      			}
      			++it;
      		}
      
      		ui.participantsList->insertTopLevelItems(0, listOfDrivers);
      

      The objects are moved in the QList, but it won't copy them in the QTreeWidget ("Participants").

      Any help is appreciated.
      Thank you!

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @Ovidiu_GCO
      Hi
      The QTreeWidgets owns the QTreeWidgetItems and im not sure they will just allow to give them to other QTreeWidgets.
      They support drag and drop between them which handle it automatically but
      doing with a >> button, i think you have to use http://doc.qt.io/qt-5/qtreewidgetitem.html#removeChild
      http://doc.qt.io/qt-5/qtreewidgetitem.html#takeChild
      to take it out of "Drives" and insert into Participants.

      O 1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @mrjj said in Moving QTreeWidgetItems between two QTreeWidgets:

        i think you have to use http://doc.qt.io/qt-5/qtreewidgetitem.html#removeChild

        takeChild() is more what you want I guess :)

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        mrjjM 1 Reply Last reply
        1
        • mrjjM mrjj

          @Ovidiu_GCO
          Hi
          The QTreeWidgets owns the QTreeWidgetItems and im not sure they will just allow to give them to other QTreeWidgets.
          They support drag and drop between them which handle it automatically but
          doing with a >> button, i think you have to use http://doc.qt.io/qt-5/qtreewidgetitem.html#removeChild
          http://doc.qt.io/qt-5/qtreewidgetitem.html#takeChild
          to take it out of "Drives" and insert into Participants.

          O Offline
          O Offline
          Ovidiu_GCO
          wrote on last edited by
          #4

          @mrjj Thank you for the Drag&Drop idea, I managed to make it work, but I still have a question.

          If I move an item between the QTreeWidgets, it loses its children. Is there a way to move them too along with the parent item?

          mrjjM 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @mrjj said in Moving QTreeWidgetItems between two QTreeWidgets:

            i think you have to use http://doc.qt.io/qt-5/qtreewidgetitem.html#removeChild

            takeChild() is more what you want I guess :)

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            That does indeed sound more like it :)
            However, RemoveChild docs says
            "The removed item will not be deleted."
            so i wonder whats the actual difference ?

            Christian EhrlicherC 1 Reply Last reply
            0
            • O Ovidiu_GCO

              @mrjj Thank you for the Drag&Drop idea, I managed to make it work, but I still have a question.

              If I move an item between the QTreeWidgets, it loses its children. Is there a way to move them too along with the parent item?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Ovidiu_GCO
              Hi
              If you also select its child and then drag/drop, are they moved too ?
              If yes, you could auto select the parents child when user click it and
              hence move them too.

              O 1 Reply Last reply
              0
              • mrjjM mrjj

                @Christian-Ehrlicher
                That does indeed sound more like it :)
                However, RemoveChild docs says
                "The removed item will not be deleted."
                so i wonder whats the actual difference ?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @mrjj said in Moving QTreeWidgetItems between two QTreeWidgets:

                so i wonder whats the actual difference ?

                takeChild() returns the pointer to the item. removeChild() does not - it's a convenience function which nobody needs since the item leaks in 99% of the use cases then.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                mrjjM O 2 Replies Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @mrjj said in Moving QTreeWidgetItems between two QTreeWidgets:

                  so i wonder whats the actual difference ?

                  takeChild() returns the pointer to the item. removeChild() does not - it's a convenience function which nobody needs since the item leaks in 99% of the use cases then.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Christian-Ehrlicher
                  doh, too little coffe :)
                  Completely missed it had no return.
                  Thank you.

                  1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @Ovidiu_GCO
                    Hi
                    If you also select its child and then drag/drop, are they moved too ?
                    If yes, you could auto select the parents child when user click it and
                    hence move them too.

                    O Offline
                    O Offline
                    Ovidiu_GCO
                    wrote on last edited by
                    #9

                    @mrjj I tried to move an item, and its children by selecting them too, from "Drivers" to "Participants" using Drag&Drop but It moved all of them as parent-items.

                    Any ideas on how to move them as a "tree" (the parent should keep its children with it).

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @mrjj said in Moving QTreeWidgetItems between two QTreeWidgets:

                      so i wonder whats the actual difference ?

                      takeChild() returns the pointer to the item. removeChild() does not - it's a convenience function which nobody needs since the item leaks in 99% of the use cases then.

                      O Offline
                      O Offline
                      Ovidiu_GCO
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher I don't really understand your approach. I understood your "takeChild()" use over the "removeChild()", but I don't even manage to move the parent-item using the ">>" button, so I don't understand how could I do it.

                      Could you provide some code snippet or at least more insight over this problem?

                      Pl45m4P 1 Reply Last reply
                      0
                      • O Ovidiu_GCO

                        @Christian-Ehrlicher I don't really understand your approach. I understood your "takeChild()" use over the "removeChild()", but I don't even manage to move the parent-item using the ">>" button, so I don't understand how could I do it.

                        Could you provide some code snippet or at least more insight over this problem?

                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on last edited by Pl45m4
                        #11

                        @Ovidiu_GCO

                        When you select a parent to move right, iterate through its childs and move them with "takeChild" to your TreeList on the right.

                        (You have to insert them, of course. "takeChild" returns a pointer to child item, as @Christian-Ehrlicher already said)

                        EDIT:
                        "takeChildren" returns a QList of ALL children. So you dont have to move them one by one.
                        You can add this QList after your parent with "insertChildren(index, QList)"

                        EDIT2:
                        Connect this with your ">>" and "<<" Button onClick-Event, get the current selected item (parent) and it should work :)

                        Im not able to test it atm (online with smartphone)


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        O 1 Reply Last reply
                        0
                        • Pl45m4P Pl45m4

                          @Ovidiu_GCO

                          When you select a parent to move right, iterate through its childs and move them with "takeChild" to your TreeList on the right.

                          (You have to insert them, of course. "takeChild" returns a pointer to child item, as @Christian-Ehrlicher already said)

                          EDIT:
                          "takeChildren" returns a QList of ALL children. So you dont have to move them one by one.
                          You can add this QList after your parent with "insertChildren(index, QList)"

                          EDIT2:
                          Connect this with your ">>" and "<<" Button onClick-Event, get the current selected item (parent) and it should work :)

                          Im not able to test it atm (online with smartphone)

                          O Offline
                          O Offline
                          Ovidiu_GCO
                          wrote on last edited by
                          #12

                          @Pl45m4 My first problem is that I am not able to move the parent. I only managed to move it using Drag&Drop, but not using the button ">>".

                          Pl45m4P 1 Reply Last reply
                          0
                          • O Ovidiu_GCO

                            @Pl45m4 My first problem is that I am not able to move the parent. I only managed to move it using Drag&Drop, but not using the button ">>".

                            Pl45m4P Offline
                            Pl45m4P Offline
                            Pl45m4
                            wrote on last edited by
                            #13

                            @Ovidiu_GCO

                            The Qt Doc says that all items are selectable, checkable... by default.
                            If you get your selected item when ">>" is clicked, it should be fine.

                            // same with move left  (<<) btn
                            connect(moveRightBtn, SIGNAL(clicked()), this, SLOT(moveItemFnct ());
                            
                            // Your move fnct:
                            void moveItem()
                            {
                               //Get current selected item and append to
                            // your list on the right
                            
                            

                            Actually there is a SIGNAL to get the clicked item + its column.
                            Check it out
                            http://doc.qt.io/qt-5/qtreewidget.html#itemPressed


                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            O 1 Reply Last reply
                            0
                            • Pl45m4P Pl45m4

                              @Ovidiu_GCO

                              The Qt Doc says that all items are selectable, checkable... by default.
                              If you get your selected item when ">>" is clicked, it should be fine.

                              // same with move left  (<<) btn
                              connect(moveRightBtn, SIGNAL(clicked()), this, SLOT(moveItemFnct ());
                              
                              // Your move fnct:
                              void moveItem()
                              {
                                 //Get current selected item and append to
                              // your list on the right
                              
                              

                              Actually there is a SIGNAL to get the clicked item + its column.
                              Check it out
                              http://doc.qt.io/qt-5/qtreewidget.html#itemPressed

                              O Offline
                              O Offline
                              Ovidiu_GCO
                              wrote on last edited by
                              #14

                              @Pl45m4 I had no doubt it should work, but it is not working. I can't even copy the parent-item to the "Participants" QTreeWidget

                              Pl45m4P 1 Reply Last reply
                              0
                              • O Ovidiu_GCO

                                @Pl45m4 I had no doubt it should work, but it is not working. I can't even copy the parent-item to the "Participants" QTreeWidget

                                Pl45m4P Offline
                                Pl45m4P Offline
                                Pl45m4
                                wrote on last edited by Pl45m4
                                #15

                                @Ovidiu_GCO

                                I just tried it... (2 TreeWidgets with only two parent items each)

                                I can access the items and get their names and stuff but they don't appear in the second TreeWidget :(
                                Even the selection by mouseClick worked and I got the name / indices from each item by pressing the button.
                                Maybe we overlook something?! Some steps to copy to a TreeWidget or maybe the items lose required flags when you "export" the items and try to add them to a different TreeWidget?!


                                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                ~E. W. Dijkstra

                                O 1 Reply Last reply
                                0
                                • Maaz MominM Offline
                                  Maaz MominM Offline
                                  Maaz Momin
                                  wrote on last edited by
                                  #16

                                  @Ovidiu_GCO Can you show the code you have written after changes mentioned.

                                  O 1 Reply Last reply
                                  0
                                  • Pl45m4P Pl45m4

                                    @Ovidiu_GCO

                                    I just tried it... (2 TreeWidgets with only two parent items each)

                                    I can access the items and get their names and stuff but they don't appear in the second TreeWidget :(
                                    Even the selection by mouseClick worked and I got the name / indices from each item by pressing the button.
                                    Maybe we overlook something?! Some steps to copy to a TreeWidget or maybe the items lose required flags when you "export" the items and try to add them to a different TreeWidget?!

                                    O Offline
                                    O Offline
                                    Ovidiu_GCO
                                    wrote on last edited by
                                    #17

                                    @Pl45m4 I know, I tried it too. I was reading a post on stackoverflow but I didn't understand all of it. As far as I understood, QTreeWidget is used for static-view and we should use QTreeView(since Qt 5), but I don't find the documentation very friendly and I am still trying to understand it.

                                    Maybe we should switch to QTreeView?

                                    1 Reply Last reply
                                    0
                                    • Maaz MominM Maaz Momin

                                      @Ovidiu_GCO Can you show the code you have written after changes mentioned.

                                      O Offline
                                      O Offline
                                      Ovidiu_GCO
                                      wrote on last edited by
                                      #18

                                      @Maaz-Momin At this moment, my code for the ">>" button is a work in-progress and I kinda messed it up, but now it looks like this:

                                      QTreeWidgetItemIterator it(ui.driversList);
                                      
                                      		while (*it) {
                                      			if ((*it)->isSelected())
                                      				ui.participantsList->addTopLevelItem((*it));
                                      			++it;
                                      		}
                                      

                                      I tried to make it copy any selected item from "Drivers" to "Participants", but there is no change in the view.

                                      I didn't try to use .takeChild() yet, because I didn't manage to move the parent using code, just by Drag&Drop.

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

                                        Hi,

                                        Out of curiosity, why are you using a tree model ?

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

                                        O 1 Reply Last reply
                                        0
                                        • SGaistS SGaist

                                          Hi,

                                          Out of curiosity, why are you using a tree model ?

                                          O Offline
                                          O Offline
                                          Ovidiu_GCO
                                          wrote on last edited by
                                          #20

                                          @SGaist Honestly, I don't really know... This is my first project using Qt (or even C++ on a more serious project). I am not familiar with it or any other framework, for that matter.

                                          It is a school project and I am trying to make it work so I can add it to my portofolio hoping that in the future I will become an intern with Qt and C++(and eventually a Junior Software Engineer).

                                          So, I use it because it is the first widget that seemed to work for me.
                                          I need to show some details about the drivers from the database and select a few of them to participate in a race.

                                          I believe I could use also a QTableView(since there are not a lot of details beside the name) with a checkbox on the last column that would tell if the driver is a participant or not, but I am still trying to make it work with QTreeWidgets.

                                          I am open to suggestions, if you have an idea that would be appropriate to a newbie like me.

                                          jsulmJ 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