[solved]Drag and Drop within a QListWidget
-
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.
-
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
-
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.
-
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 :)