Move (not copy) QListWidgetItems between QListWidgets using Drag&Drop



  • Hello,

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

    0_1502312442935_dragdrop.png

    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?

    Thanks!
    Tobias


  • Lifetime Qt Champion

    Hi,

    You have to set the drag drop mode to Move rather that DragDrop. The default drop action is likely what you are looking for.

    Drag and drop with item views.

    [Edit: corrected mixed property SGaist]



  • Hello,

    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!


  • Lifetime Qt Champion

    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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.