Drag and drop image from application to windows explorer

  • Hi guys!
    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?!

  • Qt Champions 2018

  • Lifetime Qt Champion

    and for
    "shop graphic application"
    you can use

    and openUrl

    "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.

  • Moderators


    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);
    pixmap.save(&buffer, "PNG");

    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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.