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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With