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. [solved]Drag and Drop within a QListWidget
Forum Updated to NodeBB v4.3 + New Features

[solved]Drag and Drop within a QListWidget

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.1k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    pmseattle
    wrote on last edited by
    #1

    I have drag and drop enabled in a QListWidget which is inside a QDialog, with dragDropMode set to InternalMove and defaultDropAction set to MoveAction. This allows me to rearrange the order of the items in the QListWidget using drag and drop. I would like to trigger an action in another window each time I complete a drop, but I am having difficulties. I subclassed the QListWidget and reimplemented the dropEvent in order to emit a signal ( or execute some other code ) when the drop occurs, but the item I am dropping disappears from my list widget instead of being moved to the target position. Can somebody see what I am doing wrong ? Here's the new dropEvent:

    void SpecialQListWidget::dropEvent(QDropEvent *event)
    {
    event->setDropAction(Qt::MoveAction);
    event->accept();
    SomeCode();
    }

    The dropEvent executes, but the item being moved is deleted from the QListWidget rather than being moved to the drop position.

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

      Hi,

      You are just accepting the event and not doing anything about the drop itself so it won't be done. You can just call the base class implementation to handle that for you and then your additional code.

      Hope it helps

      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
      • P Offline
        P Offline
        pmseattle
        wrote on last edited by
        #3

        Thanks, I already fixed my problem using Google - it seems that numerous other people have asked this or something similar in the past. The new version of dropEvent is:

        void SpecialListWidget::dropEvent(QDropEvent *event)
        {
        QListWidget::dropEvent(event);
        event->accept();
        emit itemDropped();
        }

        This works perfectly. And thanks to you SGaist since your reply seems to basically say the same as the above code snippet.

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

          You shouldn't need to accept the event again IIRC, it's already done in the base implementation of dropEvent.

          Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

          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
          • P Offline
            P Offline
            pmseattle
            wrote on last edited by
            #5

            You are correct, the accept is unnecessary. Thanks again!

            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