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
Forum Updated to NodeBB v4.3 + New Features

Moving QTreeWidgetItems between two QTreeWidgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
26 Posts 7 Posters 5.0k Views 3 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.
  • C Christian Ehrlicher
    6 Jan 2019, 13:38

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

    M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 6 Jan 2019, 13:39 last edited by mrjj 1 Jun 2019, 16:06
    #8

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

    1 Reply Last reply
    0
    • M mrjj
      6 Jan 2019, 13:24

      @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 6 Jan 2019, 22:53 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
      • C Christian Ehrlicher
        6 Jan 2019, 13:38

        @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 6 Jan 2019, 23:05 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?

        P 1 Reply Last reply 6 Jan 2019, 23:27
        0
        • O Ovidiu_GCO
          6 Jan 2019, 23:05

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

          P Offline
          P Offline
          Pl45m4
          wrote on 6 Jan 2019, 23:27 last edited by Pl45m4 1 Jul 2019, 00:17
          #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 6 Jan 2019, 23:42
          0
          • P Pl45m4
            6 Jan 2019, 23:27

            @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 6 Jan 2019, 23:42 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 ">>".

            P 1 Reply Last reply 7 Jan 2019, 00:13
            0
            • O Ovidiu_GCO
              6 Jan 2019, 23:42

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

              P Offline
              P Offline
              Pl45m4
              wrote on 7 Jan 2019, 00:13 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 7 Jan 2019, 00:21
              0
              • P Pl45m4
                7 Jan 2019, 00: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

                O Offline
                O Offline
                Ovidiu_GCO
                wrote on 7 Jan 2019, 00:21 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

                P 1 Reply Last reply 7 Jan 2019, 01:48
                0
                • O Ovidiu_GCO
                  7 Jan 2019, 00:21

                  @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

                  P Offline
                  P Offline
                  Pl45m4
                  wrote on 7 Jan 2019, 01:48 last edited by Pl45m4 1 Jul 2019, 08:00
                  #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 7 Jan 2019, 19:49
                  0
                  • M Offline
                    M Offline
                    Maaz Momin
                    wrote on 7 Jan 2019, 03:56 last edited by
                    #16

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

                    O 1 Reply Last reply 7 Jan 2019, 20:10
                    0
                    • P Pl45m4
                      7 Jan 2019, 01:48

                      @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 7 Jan 2019, 19:49 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
                      • M Maaz Momin
                        7 Jan 2019, 03:56

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

                        O Offline
                        O Offline
                        Ovidiu_GCO
                        wrote on 7 Jan 2019, 20:10 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
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 7 Jan 2019, 20:22 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 7 Jan 2019, 23:18
                          0
                          • S SGaist
                            7 Jan 2019, 20:22

                            Hi,

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

                            O Offline
                            O Offline
                            Ovidiu_GCO
                            wrote on 7 Jan 2019, 23:18 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.

                            J 1 Reply Last reply 8 Jan 2019, 05:20
                            0
                            • O Ovidiu_GCO
                              7 Jan 2019, 23:18

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

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 8 Jan 2019, 05:20 last edited by
                              #21

                              @Ovidiu_GCO Do you really need a tree structure? If not you could simply use QListWidget.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              O 1 Reply Last reply 8 Jan 2019, 21:32
                              1
                              • J jsulm
                                8 Jan 2019, 05:20

                                @Ovidiu_GCO Do you really need a tree structure? If not you could simply use QListWidget.

                                O Offline
                                O Offline
                                Ovidiu_GCO
                                wrote on 8 Jan 2019, 21:32 last edited by
                                #22

                                @jsulm I have to display a driver, his car and some specs for it. I think I will try a QTableView/QTableWidget

                                M 1 Reply Last reply 8 Jan 2019, 21:50
                                0
                                • O Ovidiu_GCO
                                  8 Jan 2019, 21:32

                                  @jsulm I have to display a driver, his car and some specs for it. I think I will try a QTableView/QTableWidget

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 8 Jan 2019, 21:50 last edited by
                                  #23

                                  @Ovidiu_GCO
                                  Hi
                                  Would a driver have more than one car,
                                  or could the the information be presented as one item ?
                                  like
                                  alt text

                                  O 1 Reply Last reply 8 Jan 2019, 22:32
                                  0
                                  • M mrjj
                                    8 Jan 2019, 21:50

                                    @Ovidiu_GCO
                                    Hi
                                    Would a driver have more than one car,
                                    or could the the information be presented as one item ?
                                    like
                                    alt text

                                    O Offline
                                    O Offline
                                    Ovidiu_GCO
                                    wrote on 8 Jan 2019, 22:32 last edited by
                                    #24

                                    @mrjj There could be more entries of the same driver with different cars, yes.

                                    I will try the QTableWidget approach, but I already stepped in other problems with it. (I will make a new post since they are not connected).

                                    Thank you all for your time and advices! ;)

                                    M J 2 Replies Last reply 8 Jan 2019, 22:58
                                    0
                                    • O Ovidiu_GCO
                                      8 Jan 2019, 22:32

                                      @mrjj There could be more entries of the same driver with different cars, yes.

                                      I will try the QTableWidget approach, but I already stepped in other problems with it. (I will make a new post since they are not connected).

                                      Thank you all for your time and advices! ;)

                                      M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 8 Jan 2019, 22:58 last edited by mrjj 1 Aug 2019, 23:00
                                      #25

                                      Hi
                                      Ok, so it really is a tree/table structure.
                                      -Driver
                                      --Car
                                      stats
                                      --Car
                                      Stats

                                      But i do wonder when you then select a driver, all his cars should
                                      also be shown in the other list or just one of the cars ? (used for that race)

                                      I liked this info
                                      https://wiki.qt.io/How_to_Use_QTableWidget

                                      when starting using QTableWidget

                                      1 Reply Last reply
                                      0
                                      • O Ovidiu_GCO
                                        8 Jan 2019, 22:32

                                        @mrjj There could be more entries of the same driver with different cars, yes.

                                        I will try the QTableWidget approach, but I already stepped in other problems with it. (I will make a new post since they are not connected).

                                        Thank you all for your time and advices! ;)

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 9 Jan 2019, 05:20 last edited by
                                        #26

                                        @Ovidiu_GCO As alternative you could use several list widgets:

                                        1. On the left is a list of drivers
                                        2. To the right side of the first list you have a second one: when you select a driver you put the cars of that driver into this list
                                        3. More lists if needed...

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0

                                        17/26

                                        7 Jan 2019, 19:49

                                        • Login

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