Reputation: 59
I'm trying to create a Push Button in my Qt5 application which can be dragged outside the application and when dropped, would copy a file to that location.
Take Google Chrome browser, for example. When a file is downloaded, it could be dragged from the list which appears on the bottom of the window to any other places, like a directory.
Is there any specialized widget to accomplish this task or do I have to write one on my own? If so, how? I'm a capable C++ programmer but not much experienced with Qt framework.
Upvotes: 0
Views: 1378
Reputation: 561
Did you try Draggable Text Example?
In the dragwidget.cpp file, if you look a the function mousePressEvent, you could see that QDrag need mimeData
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
Mimedata define your QDrag behaviour, like if you drag an image, file or text or anything else.
If you want to tell windows that you have file in your drag, you need to add an URL in your mimedata with something like :
mimeData->setUrls(QList<QUrl>() << QUrl::fromLocalFile("D:/test.txt"));
In draggabletext example, the mimedata contain text, you could see that at line [158:161].
But if you replace the existing :
mimeData->setText(child->text());
mimeData->setData(hotSpotMimeDataKey(),
QByteArray::number(hotSpot.x()) + ' ' +
QByteArray::number(hotSpot.y()));
by
mimeData->setUrls(QList<QUrl>() << QUrl::fromLocalFile("D:/test.txt"));
when you drag the text in the windows explorer, windows will accept it and copy the file where you drop it.
Upvotes: 4