在 QGraphicsScene 上接受 drop [英] Accepting drops on a QGraphicsScene

查看:82
本文介绍了在 QGraphicsScene 上接受 drop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 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());
}

场景仍然不会接受掉落.我猜那是因为我不能在我的 QGraphicsScene 上执行 setAcceptDrops(true).

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?

推荐答案

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

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!

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

干杯

这篇关于在 QGraphicsScene 上接受 drop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆