Drag and drop image from application to windows explorer
AliReza Beytari last edited by AliReza Beytari
There is a QLabel on my ui form which is as an image (I set "pixmap" property). So now, I want to let the use to drag and drop this image to Windows Explorer and shop graphic application (like Photoshop). How to do that?!
Did you read http://doc.qt.io/qt-5.6/dnd.html ?
"shop graphic application"
you can use
"If the URL is a reference to a local file (i.e., the URL scheme is "file") then it will be opened with a suitable application instead of a Web browser."
That will open the app user has for the image type.
So to enable drops to the filesystem you should add a file urls (using QMimeData::setUrls() for example).
For photoshop it might be enough to set a image on the mimedata (using QMimeData::setPixmap())
Take a look at the Delayed encoding example.
The interesting part here is the QMimeData::retrieveData() overload. This method gets called when a drop happens.
So when a drop to the filesystem happens the mimedata requests the data and calls retrieveData(). In there you write your pixmap data to a temporary image file. For that you can use QTemporaryFile class for example. Write the data to the temporary file and return the url to the file. Mime-Type: "text/uri-list"
Analog for the pixmap image. Simply return the QPixmap. Mime-Type: "image/png"
Actually it could also be necessary to return the PNG binary data. I am not sure. This can be done like this:
QPixmap pixmap; QByteArray data; QBuffer buffer(&data); buffer.open(QIODevice::WriteOnly); pixmap.save(&buffer, "PNG"); buffer.close();
The advantage of this approach is that you only create the drop data when necessary. Especially the temp file used for drops to the filesystem.