Drag n Droop functionality in db mapped QLabel

  • I 'm trying to add Drag n Drop functionality in a QLabel.
    I subclass it and promote a QLabel to my class.

    My header is this:

    #ifndef IMAGELABEL_H
    #define IMAGELABEL_H
    #include <QWidget>
    #include <QLabel>
    class QMimeData;
    class ImageLabel : public QLabel
        ImageLabel(QWidget *parent = 0);
        void changed(const QMimeData *mimeData = 0);
    public slots:
        void clear();
        //virtual void dropEvent(QDropEvent *event);
        void dropEvent(QDropEvent *event) override;
        void dragEnterEvent(QDragEnterEvent *event) override;
        void dragMoveEvent(QDragMoveEvent *event) override;
        void dragLeaveEvent(QDragLeaveEvent *event) override;
        QLabel *label;
    #endif // IMAGELABEL_H

    My ccp file is this:

    #include "imagelabel.h"
    #include <QDragEnterEvent>
    #include <QDropEvent>
    #include <QMimeData>
    #include <QDebug>
    #include <QPixmap>
    #include <Qt>
    ImageLabel::ImageLabel(QWidget *parent) :
        qDebug()<< "ImageLabel::ImageLabel !!!!!!";
    void ImageLabel::dropEvent(QDropEvent *event)
        const QMimeData *mimeData = event->mimeData();
        if (mimeData->hasImage()) {
            qDebug()<< "ImageLabel::dropEvent mimeData->hasImage";
        } else if (mimeData->hasHtml()) {
            qDebug()<< "ImageLabel::dropEvent mimeData->hasHtml";
        } else if (mimeData->hasText()) {
            qDebug()<< "ImageLabel::dropEvent mimeData->hasText" << " " << mimeData->text();
            QString subStr = QLatin1String("file:///");
            QLatin1String newStr = QLatin1String("");
            QString mimeDataText = QString(mimeData->text().replace(mimeData->text().indexOf(subStr), subStr.size(), newStr));
            QPixmap pixmap(mimeDataText);
            pixmap = pixmap.scaled(width(), height(), Qt::KeepAspectRatio);
        } else if (mimeData->hasUrls()) {
            QList<QUrl> urlList = mimeData->urls();
            QString text;
            qDebug()<< "ImageLabel::dropEvent mimeData->hasUrls";
            for (int i = 0; i < urlList.size() && i < 32; ++i)
                text += urlList.at(i).path() + QLatin1Char('\n');
        } else {
            setText(tr("Cannot display data"));
    void ImageLabel::dragEnterEvent(QDragEnterEvent *event)
        setText(tr("<drop content>"));
        emit changed(event->mimeData());
         qDebug()<< "ImageLabel::dragEnterEvent!";
    void ImageLabel::dragMoveEvent(QDragMoveEvent *event)
    void ImageLabel::dragLeaveEvent(QDragLeaveEvent *event)
    void ImageLabel::clear()
        setText(tr("<drop content>"));
        emit changed();

    But when I drop an image in my label, then mimeData->hasImage() returns false, while it should be true.
    Instead mimeData->hasText() returns true!!!

    That 's why I create an image out of mimeData text: QPixmap pixmap(mimeDataText);

    Why mimeData->hasImage() returns false?

  • Lifetime Qt Champion


    From where is this image coming from ?

    If you are not creating the mime data yourself, you're likely getting an URI to where the image is coming from.

  • The image is from my pc.
    mimeData->hasHtml() returns false.

  • Lifetime Qt Champion

    HTML and URI are two different things. URI is an address to a resource online or offline. HTML is just formatted text.

    Then, did you try to print the text you received ?

  • A i have already mentioned:
    @Panoss said in Drag n Droop functionality in db mapped QLabel:

    That 's why I create an image out of mimeData text: QPixmap pixmap(mimeDataText);

    I use the text (which is the path to the file, the image) to create a QPixmap.

    I just think that the correct would be:
    mimeData->hasImage() to return true.
    Create a QPixmap wtih: mimeData->imageData().

    Is it normal mimeData->hasImage() to return false, when I drop an image?

  • Lifetime Qt Champion

    Yes it's normal, the fact that you are dragging an image file around doesn't mean that the mime data needs to contain that image loaded in RAM.

    The path to the original file makes much more sense for further processing whether it is moving the file from one folder to another or in your case showing it on a label.

    Imagine for a second that you are dragging around a VirtualBox virtual machine hard drive file. Would you really want to have the whole virtual disk loaded in memory while you are dragging and dropping it ?

  • Lifetime Qt Champion

    Note that doesn't mean you can't create mime data with images embedded in it, I was just talking about the interaction from your desktop environment to your application in that precise use case.

  • Thank you for your time SGaist, have a nice day.

Log in to reply

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