Accepting drops on a QGraphicsScene

人盡茶涼 提交于 2019-12-10 12:56:18

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/4177720/accepting-drops-on-a-qgraphicsscene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!