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 { Q_OBJECT public: ImageLabel(QWidget *parent = 0); signals: void changed(const QMimeData *mimeData = 0); public slots: void clear(); protected: //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; private: 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) : QLabel(parent) { setAcceptDrops(true); qDebug()<< "ImageLabel::ImageLabel !!!!!!"; } void ImageLabel::dropEvent(QDropEvent *event) { const QMimeData *mimeData = event->mimeData(); if (mimeData->hasImage()) { setPixmap(qvariant_cast<QPixmap>(mimeData->imageData())); qDebug()<< "ImageLabel::dropEvent mimeData->hasImage"; } else if (mimeData->hasHtml()) { setText(mimeData->html()); setTextFormat(Qt::RichText); qDebug()<< "ImageLabel::dropEvent mimeData->hasHtml"; } else if (mimeData->hasText()) { setText(mimeData->text()); setTextFormat(Qt::PlainText); 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); setPixmap(pixmap); } 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'); setText(text); } else { setText(tr("Cannot display data")); } setBackgroundRole(QPalette::Dark); event->acceptProposedAction(); } void ImageLabel::dragEnterEvent(QDragEnterEvent *event) { setText(tr("<drop content>")); setBackgroundRole(QPalette::Highlight); event->acceptProposedAction(); emit changed(event->mimeData()); qDebug()<< "ImageLabel::dragEnterEvent!"; } void ImageLabel::dragMoveEvent(QDragMoveEvent *event) { event->acceptProposedAction(); } void ImageLabel::dragLeaveEvent(QDragLeaveEvent *event) { clear(); event->accept(); } void ImageLabel::clear() { setText(tr("<drop content>")); setBackgroundRole(QPalette::Dark); 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?
-
Hi,
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.
-
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 ?
-
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 { Q_OBJECT public: ImageLabel(QWidget *parent = 0); signals: void changed(const QMimeData *mimeData = 0); public slots: void clear(); protected: //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; private: 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) : QLabel(parent) { setAcceptDrops(true); qDebug()<< "ImageLabel::ImageLabel !!!!!!"; } void ImageLabel::dropEvent(QDropEvent *event) { const QMimeData *mimeData = event->mimeData(); if (mimeData->hasImage()) { setPixmap(qvariant_cast<QPixmap>(mimeData->imageData())); qDebug()<< "ImageLabel::dropEvent mimeData->hasImage"; } else if (mimeData->hasHtml()) { setText(mimeData->html()); setTextFormat(Qt::RichText); qDebug()<< "ImageLabel::dropEvent mimeData->hasHtml"; } else if (mimeData->hasText()) { setText(mimeData->text()); setTextFormat(Qt::PlainText); 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); setPixmap(pixmap); } 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'); setText(text); } else { setText(tr("Cannot display data")); } setBackgroundRole(QPalette::Dark); event->acceptProposedAction(); } void ImageLabel::dragEnterEvent(QDragEnterEvent *event) { setText(tr("<drop content>")); setBackgroundRole(QPalette::Highlight); event->acceptProposedAction(); emit changed(event->mimeData()); qDebug()<< "ImageLabel::dragEnterEvent!"; } void ImageLabel::dragMoveEvent(QDragMoveEvent *event) { event->acceptProposedAction(); } void ImageLabel::dragLeaveEvent(QDragLeaveEvent *event) { clear(); event->accept(); } void ImageLabel::clear() { setText(tr("<drop content>")); setBackgroundRole(QPalette::Dark); 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?
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?
-
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 ?
-
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.