我正在尝试实现拖放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)
on my QGraphicsScene
.
如何接受图形场景中的掉落?
这里的技巧是还要接受 QGraphicsScene::dragMoveEvent() 中的事件!
原因是如果鼠标下没有项目,默认实现会忽略拖放事件!
另请参阅:http://www.qtcentre.org/threads/8022-QGraphicsScene-doesn-t-accept-Drops
Cheers
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)