1

I have searched the web for possibilities to realize this but haven't found a solution. Is there a simple way of removing an item from the list that is dropped on a non-receiving area or even outside the application's window?

So far I accept the delete key for removing items by means of a shortcut:

QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_Delete), myList); shortcut->setContext(Qt::WidgetShortcut); connect(shortcut, SIGNAL(activated()), this, SLOT(deleteSelection())); 

But since I add items to the list via drag and drop, I would like to be able to remove them in the same way, too. The items in the list can also be ordered via drag and drop.

Any hints or links are appreciated.

3
  • Remove them on dragLeaveEvent instead of dropEvent Commented Feb 13, 2012 at 8:57
  • @KamilKlimek So you think I would have to subclass the widget to be able to do that? Is there no other way? I would have thought that was a pretty standard thing to want from a QListWidget. Commented Feb 20, 2012 at 21:18
  • 1
    Try installing event filter and catch dropEvent from it Commented Feb 21, 2012 at 22:43

1 Answer 1

1

This seems to work for me:

  • The drag-and-drop action should be Qt::MoveAction
  • Ensure the parent of the QListWidget (e.g., QDialog) has the following:
    • dragEnterEvent() implemented
    • dropEvent() implemented (ignore the mime data here)
    • setAcceptDrops(true);

Ignoring the mime data in dropEvent() in a Qt::MoveAction should be equivalent to a item remove operation.

Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

In the Qt Designer, I set all properties to what you suggested and installed an event filter on the list widget, as Kamil said, to catch the dragEnter/dragLeave/dropEvents but the occasions on which an event is fired or not seem almost random to me. The dragLeaveEvent for example doesn't even seem to get fired at all. The event filter approach makes sense to me and the events being fired or not seems a very Qt specific issue, so I posted a more detailed question here, in case you're interested. Thanks for your help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.