10

我正在尝试为QGraphicsScene. 以下是我重载的事件:

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());
}

现场仍然不会接受滴剂。我猜那是因为我setAcceptDrops(true)QGraphicsScene.

如何接受图形场景中的水滴?

4

1 回答 1

14

这里的技巧是还接受 QGraphicsScene::dragMoveEvent() 中的事件!

原因是默认实现,如果鼠标下没有项目,它会忽略拖放事件!

另请参阅:http ://www.qtcentre.org/threads/8022-QGraphicsScene-doesn-t-accept-Drops

干杯

于 2010-12-12T13:18:33.707 回答