Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accepting drops on a QGraphicsScene

I'm trying to implement drag'n'drop for a QGraphicsScene. Here are the events I've overloaded:

void TargetScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
    bool acceptDrag = false;
    const QMimeData* mime = event->mimeData();

    // Is an image present?
    if (mime->hasImage()) {
        QImage img = qvariant_cast<QImage>(mime->imageData());
        dragPix = QPixmap::fromImage(img);
        acceptDrag = !dragPix.isNull();
    }

    event->setAccepted(acceptDrag);
}

void TargetScene::dropEvent(QGraphicsSceneDragDropEvent *event) {
    // Add dragged pixmap to scene
    QGraphicsPixmapItem* newPix = this->addPixmap(dragPix);
    newPix->setPos(event->pos().x(), event->pos().y());
}

The scene still won't accept drops. I'm guessing that's because I can't do setAcceptDrops(true) on my QGraphicsScene.

How do I accept drops on a graphics scene?

like image 945
Pieter Avatar asked Nov 14 '10 13:11

Pieter


1 Answers

The trick here is to ALSO accept the event in the QGraphicsScene::dragMoveEvent()!

The reason is the DEFAULT implementation which ignores drag and drop events if there is no item under the mouse!

Also refer to: http://www.qtcentre.org/threads/8022-QGraphicsScene-doesn-t-accept-Drops

Cheers

like image 97
Oliver Knoll Avatar answered Sep 26 '22 01:09

Oliver Knoll