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. Draging a ListWidget item's icon to a label?
Qt 6.11 is out! See what's new in the release blog

Draging a ListWidget item's icon to a label?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.8k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    MrVirtualCoder
    wrote on last edited by
    #1

    I'm completely confused as to how i am going to go about doing this.
    Any suggestions would be very much appreciated.

    To further explain: I'm going about generating several Listwidget items, which all contains their own unique icon, associated with text(already constructed this);
    i want to be able to drag these items from (the Listwidget) and to (the Label); where-after the label will be populated with the chosen-dragged item's icon.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Did you read the Drag & Drop documentation in Qt?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MrVirtualCoder
        wrote on last edited by
        #3

        [quote author="Andre" date="1325616742"]Did you read the Drag & Drop documentation in Qt?[/quote]

        Yes, yes i did.
        I even tried it, but i can't seem to make it work.
        The part i am getting wrong is when i have to actually drag an Item from my listwidget TO my label. I'm not sure as how i am to go about generating such event, properly.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by
          #4

          http://www.java2s.com/Code/Cpp/Qt/QListWidgetdraganddrop.htm

          1 Reply Last reply
          0
          • B Offline
            B Offline
            broadpeak
            wrote on last edited by
            #5

            The drag&drop logic is the following (something like similar to this):

            Drag site:

            @
            QMimeData* prepareListWidgetItem( const QString& path )
            {
            QImage pic(path);
            QMimeData *mimeData = new QMimeData;
            mimeData->setImageData( pic );
            QList<QUrl> urls;
            QUrl imageUrl( path );
            urls.append( imageUrl );
            mimeData->setUrls( urls );
            mimeData->setText( imageUrl.path() );
            return mimeData;
            }

            void DragLabel::mousePressEvent(QMouseEvent event)
            {
            if (event->button() == Qt::LeftButton) {
            QMimeData
            data = prepareListWidgetItem(picPath);
            QDrag *drag = new QDrag(this);
            drag->setMimeData(data);
            if (pixmap())
            drag->setPixmap(pixmap()->
            scaled(100,100, Qt::KeepAspectRatio));
            drag->start();
            }
            }

            @

            Drop site:

            @
            DropLabel::DropLabel(QWidget *parent)
            : QLabel(parent)
            {
            setAcceptDrops(true);
            }

            void DropLabel::dragEnterEvent(QDragEnterEvent event)
            {
            if (event && event->mimeData()) {
            const QMimeData
            md = event->mimeData();
            if (md->hasImage() || md->hasUrls() || md->hasText())
            event->acceptProposedAction();
            }
            }

            void DropLabel::dropEvent(QDropEvent *event)
            {
            QPixmap pix;
            if(event && event->mimeData()) {
            const QMimeData *data = event->mimeData();
            if (data->hasImage())
            pix = data->imageData().value<QPixmap>();
            else if(data->hasUrls())
            foreach(QUrl url, data->urls()) {
            QFileInfo info(url.toLocalFile());
            if(info.exists() && info.isFile())
            pix = QPixmap(url.toLocalFile());
            if (pixmap() && !pixmap()->isNull())
            break;
            }
            else if(data->hasText()) {
            QUrl url(data->text());
            QFileInfo info(url.toLocalFile());
            if(info.exists() && info.isFile())
            pix = QPixmap(url.toLocalFile());
            }
            }
            if (!pix.isNull()) {
            setPixmap(pix);
            resize(pix.size());
            }
            }
            @

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MrVirtualCoder
              wrote on last edited by
              #6

              [quote author="broadpeak" date="1325619302"]
              The drag&drop logic is the following (something like similar to this):

              Drag site:

              @
              QMimeData* prepareListWidgetItem( const QString& path )
              {
              QImage pic(path);
              QMimeData *mimeData = new QMimeData;
              mimeData->setImageData( pic );
              QList<QUrl> urls;
              QUrl imageUrl( path );
              urls.append( imageUrl );
              mimeData->setUrls( urls );
              mimeData->setText( imageUrl.path() );
              return mimeData;
              }

              void DragLabel::mousePressEvent(QMouseEvent event)
              {
              if (event->button() == Qt::LeftButton) {
              QMimeData
              data = prepareListWidgetItem(picPath);
              QDrag *drag = new QDrag(this);
              drag->setMimeData(data);
              if (pixmap())
              drag->setPixmap(pixmap()->
              scaled(100,100, Qt::KeepAspectRatio));
              drag->start();
              }
              }

              @

              Drop site:

              @
              DropLabel::DropLabel(QWidget *parent)
              : QLabel(parent)
              {
              setAcceptDrops(true);
              }

              void DropLabel::dragEnterEvent(QDragEnterEvent event)
              {
              if (event && event->mimeData()) {
              const QMimeData
              md = event->mimeData();
              if (md->hasImage() || md->hasUrls() || md->hasText())
              event->acceptProposedAction();
              }
              }

              void DropLabel::dropEvent(QDropEvent *event)
              {
              QPixmap pix;
              if(event && event->mimeData()) {
              const QMimeData *data = event->mimeData();
              if (data->hasImage())
              pix = data->imageData().value<QPixmap>();
              else if(data->hasUrls())
              foreach(QUrl url, data->urls()) {
              QFileInfo info(url.toLocalFile());
              if(info.exists() && info.isFile())
              pix = QPixmap(url.toLocalFile());
              if (pixmap() && !pixmap()->isNull())
              break;
              }
              else if(data->hasText()) {
              QUrl url(data->text());
              QFileInfo info(url.toLocalFile());
              if(info.exists() && info.isFile())
              pix = QPixmap(url.toLocalFile());
              }
              }
              if (!pix.isNull()) {
              setPixmap(pix);
              resize(pix.size());
              }
              }
              @[/quote]
              I thank you for your example, but i was merely looking for the logic and the code behind ACTING upon the actual event of having dragged a ListWidget item over a label. Rest i can do myself.

              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