PDA

View Full Version : Drag and Drop without subclass a widget.



cydside
12th January 2013, 13:47
Hi to all,
I've a form that contains Qlinedits, Qlabels and a QListWidget. All the components are well positioned on a form, but now I've to manage the Drag and Drop of files on that QListWidget. So, I read about that issue and the only examples I've found are about subclassing the target widget. Is it possible to manage the Drag and Drop without subclassing(and manually handle the layout) the QListWidget?
Thanks.

Santosh Reddy
12th January 2013, 15:18
Yes it is indeed possible, using event filters.

cydside
12th January 2013, 15:50
Could you please post a pseudo code or suggest an implementation?

cydside
12th January 2013, 17:59
OK, solved. Here is the job I've done:



// Form Header (frmSetBackupDir class)

// Added
protected:
bool eventFilter(QObject *obj, QEvent *event);




// Form Implementation

// Added in the CTOR
ui->lneDirBU->installEventFilter(this);
ui->lneDirBU->setAcceptDrops(true);




bool frmSetBackupDir::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::DragEnter)
{
QDragEnterEvent *tDragEnterEvent = static_cast<QDragEnterEvent *>(event);
tDragEnterEvent->acceptProposedAction();

return true;
}
else if (event->type() == QEvent::DragMove)
{
QDragMoveEvent *tDragMoveEvent = static_cast<QDragMoveEvent *>(event);
tDragMoveEvent->acceptProposedAction();

return true;
}
else if (event->type() == QEvent::Drop)
{
QDropEvent *tDropEvent = static_cast<QDropEvent *>(event);
tDropEvent->acceptProposedAction();

qDebug() << "OK, execute your task!";

return true;
}
else
{
// standard event processing
return QObject::eventFilter(obj, event);
}
}


It seems to work. Better implementations are well accepted!