Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Move (not copy) QListWidgetItems between QListWidgets using Drag&Drop
I have this GUI with 3 separate QListWidgets. The 'Users' list is populated by a database that contains all the available users on the system and the idea is to drag them from this list and drop them either into the 'Managers' list or into the 'Admins' list. It should also be possible to move users from 'Admins' to 'Managers', 'Managers' to 'Admins', and from both back to 'Users'.
Here is the code snippet for the three lists:
m_pUsersLW = new QListWidget(this); m_pUsersLW->setSelectionMode(QAbstractItemView::SingleSelection); m_pUsersLW->setDragEnabled(true); m_pUsersLW->setDragDropMode(QAbstractItemView::DragDrop); m_pUsersLW->viewport()->setAcceptDrops(true); m_pUsersLW->setDropIndicatorShown(true); m_pManagersLW = new QListWidget(this); m_pManagersLW->setSelectionMode(QAbstractItemView::SingleSelection); m_pManagersLW->setDragEnabled(true); m_pManagersLW->setDragDropMode(QAbstractItemView::DragDrop); m_pManagersLW->viewport()->setAcceptDrops(true); m_pManagersLW->setDropIndicatorShown(true); m_pAdminsLW = new QListWidget(this); m_pAdminsLW->setSelectionMode(QAbstractItemView::SingleSelection); m_pAdminsLW->setDragEnabled(true); m_pAdminsLW->setDragDropMode(QAbstractItemView::DragDrop); m_pAdminsLW->viewport()->setAcceptDrops(true); m_pAdminsLW->setDropIndicatorShown(true);
Regular Drap&Drop, however, makes a copy of each item that is dragged and dropped, so when dragging e.g. User1 from Users to Managers, it is now shown twice. I have tried several combinations of the DragDropMode and I came to the conclusion, that I really would need something like a QAbstractItemView::Move mode. When I set the Users list to InternalMove, items are removed as I drag them to a different list, but I can't move anything back.
How do I get to the desired behavior?
You have to set the drag drop mode to Move rather that DragDrop.The default drop action is likely what you are looking for.
[Edit: corrected mixed property SGaist]
that is exactly what I'm trying. But there is no move mode among the Qt DragDropMode list:
QAbstractItemView::NoDragDrop 0 Does not support dragging or dropping. QAbstractItemView::DragOnly 1 The view supports dragging of its own items QAbstractItemView::DropOnly 2 The view accepts drops QAbstractItemView::DragDrop 3 The view supports both dragging and dropping QAbstractItemView::InternalMove 4 The view accepts move (not copy) operations only from itself.
The InternalMove mode allows to move items from one list to another (provided the receiving list allows drops). But it won't allow items to return to the original list.
Is there another way to achieve that? When reading the document about Drag&Drop with Model/View it looks like I'd have to switch from ListWidget to ListView and implement my own Model, which seems a lot.
This is my first time working with the Qt Drag&Drop feature, so some example code or a more detailed description on what to do would be really helpful!
Sorry, my bad, I mixed that with the handling of the drop action.
Try setting the default drop action.
Thanks, that was exactly what I was looking for. Works like a charm now!