Moving QTreeWidgetItems between two QTreeWidgets
-
Hello!
I am trying to move some
QTreeWidgetItems
between twoQTreeWidgets
(from "Drivers" to "Participants") using ">>" button, as seen in the image below.I already tried to make use of
QList<QTreeWidgetItem *>
along with aQTreeWidgetItemIterator
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 theQTreeWidget
("Participants").Any help is appreciated.
Thank you! -
@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 usehttp://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. -
@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 :)
-
@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 ? -
@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. -
@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.
-
@Christian-Ehrlicher
doh, too little coffe :)
Completely missed it had no return.
Thank you. -
@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).
-
@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?
-
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)
-
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 -
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?! -
@Ovidiu_GCO Can you show the code you have written after changes mentioned.
-
@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?
-
@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. -
Hi,
Out of curiosity, why are you using a tree model ?
-
@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.