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. Drop image into a QLabel
Forum Updated to NodeBB v4.3 + New Features

Drop image into a QLabel

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 6.7k 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.
  • J Offline
    J Offline
    JessyNay
    wrote on last edited by
    #1

    Hi, I need to drop an external Image into a QLabel.

    I made a class called LblImage, it implements dropEvent

    @
    void dropEvent(QDropEvent event)
    {
    if (this->pixmap()->isNull()){
    if (event->mimeData()->hasImage()) {
    event->accept();
    event->setDropAction(Qt::MoveAction);
    QByteArray pieceData = event->mimeData()->data("image/
    ");
    QPixmap img;
    img.loadFromData(pieceData);
    setPixmap(img);
    } else {
    event->ignore();
    }
    }
    }
    @

    And in the constructor
    @
    setAcceptDrops(true);
    @
    setAcceptDrops(true);
    If I try to drag and drop an image it does nothing, what am I missing? Thanks!

    Edit: I'm dragging from a file manager, so I receive text, I updated the method

    @
    void dropEvent(QDropEvent event){
    if (event->mimeData()->hasImage()){
    event->accept();
    event->setDropAction(Qt::MoveAction);
    QByteArray pieceData = event->mimeData()->data("image/
    ");
    QPixmap img;
    img.loadFromData(pieceData);
    setPixmap(img);
    setText(pieceData);

        }if (event->mimeData()->hasText()){
            event->accept();
            event->setDropAction(Qt::MoveAction);
            QByteArray pieceData = event->mimeData()->data("text/plain");
            QPixmap img;
            img.load(pieceData.mid(pieceData.indexOf("file://")+7));
            setPixmap(img);
        }
        else {
            event->ignore();
        }
    
    }
    

    @

    But Still it's now loading my Image, how should I load it to the label?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p-himik
      wrote on last edited by
      #2

      Here is some code from my old app:
      @void ProductImage::dragEnterEvent( QDragEnterEvent* event )
      {
      if( event->mimeData()->hasImage()
      || event->mimeData()->hasFormat( "text/uri-list" ) )
      {
      event->acceptProposedAction();
      }
      }

      void ProductImage::dropEvent( QDropEvent* event )
      {
      getImagesFromMimeData( event->mimeData() );
      event->acceptProposedAction();
      }

      void ProductImage::getImagesFromMimeData( const QMimeData* mimeData )
      {
      if( mimeData->hasImage() )
      {
      addImage( QPixmap::fromImage( qvariant_cast< QImage >( mimeData->imageData() ) ) );
      }
      else if( mimeData->hasUrls() )
      {
      foreach( QUrl url, mimeData->urls() )
      {
      addImage( QPixmap( url.path() ) );
      }
      }
      }@

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        Mime type "image/*" is not valid. I'm pretty sure you have something like "image/jpeg" or the like. You can use the dropsite app from src/examples/draganddrop to look what's actually inside the drop.

        Anyways, I would use method "QMimeData::imageData() ":http://doc.qt.nokia.com/4.7/qmimedata.html#imageData to extract the image from the drop data.

        http://www.catb.org/~esr/faqs/smart-questions.html

        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