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. Move (not copy) QListWidgetItems between QListWidgets using Drag&Drop
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
drag&dropmoveqlistwidget
5 Posts 2 Posters 5.1k Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      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]

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Sorry, my bad, I mixed that with the handling of the drop action.

          Try setting the default drop action.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Thanks, that was exactly what I was looking for. Works like a charm now!

            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