Recently I was working on a Qt app requiring Drag and Drop support. The documentation says that to use this on a QWidget, we need to set
dialog.setAcceptDrops(true);
I tried that but the cursor still showed the "not -allowed" symbol. This was bit confusing making me think that there's some internal problem. But actually the implementation of the following event handlers is mandatory to make it work-
void Dialog::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
In Qt, when an object is dropped onto a widget the following event handler is to be used to get the list of URLs of the dropped objects-
void Dialog::dropEvent(QDropEvent *event)
{
if (mimeData->hasUrls()) {
QList urlList = mimeData->urls();
qDebug() << urlList.at(0).path();
}
}
This code, according to the Drop Site example in Qt Demo (see below) should print the filename of the first file dropped.
Following is the output-
1. On Linux
/home/user/Desktop/pic1.jpg