Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag n Droop functionality in db mapped QLabel
QtWS25 Last Chance

Drag n Droop functionality in db mapped QLabel

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Panoss
    wrote on last edited by Panoss
    #1

    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?

    P 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Panoss
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • P Panoss

            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?

            P Offline
            P Offline
            Panoss
            wrote on last edited by Panoss
            #5

            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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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 ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • P Offline
                  P Offline
                  Panoss
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved