Draging a ListWidget item's icon to a label?



  • 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.



  • Did you read the Drag & Drop documentation in Qt?



  • [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.





  • 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 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.


Log in to reply
 

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