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.
  • 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
                        • O Ovidiu_GCO

                          @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 Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 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
                          1
                          • jsulmJ jsulm

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

                            O Offline
                            O Offline
                            Ovidiu_GCO
                            wrote on 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

                            mrjjM 1 Reply Last reply
                            0
                            • O Ovidiu_GCO

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

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 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
                              0
                              • mrjjM mrjj

                                @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 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! ;)

                                mrjjM jsulmJ 2 Replies Last reply
                                0
                                • O Ovidiu_GCO

                                  @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! ;)

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by mrjj
                                  #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

                                    @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! ;)

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 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

                                    • Login

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