Moving QTreeWidgetItems between two QTreeWidgets
-
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.
-
@Ovidiu_GCO Do you really need a tree structure? If not you could simply use QListWidget.
-
@Ovidiu_GCO
Hi
Would a driver have more than one car,
or could the the information be presented as one item ?
like
-
@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! ;)
-
Hi
Ok, so it really is a tree/table structure.
-Driver
--Car
stats
--Car
StatsBut 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_QTableWidgetwhen starting using QTableWidget
-
@Ovidiu_GCO As alternative you could use several list widgets:
- On the left is a list of drivers
- 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
- More lists if needed...