接受QGraphicsScene上的滴 [英] Accepting drops on a QGraphicsScene

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

问题描述

我正在尝试执行drag'n'drop一个 QGraphicsScene 。以下是我已经重载的事件:

  void TargetScene :: dragEnterEvent(QGraphicsSceneDragDropEvent * event){
bool acceptDrag = false;
const QMimeData * mime = event-> mimeData();

//是否存在图像?
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){
//将拖动的像素图添加到场景
QGraphicsPixmapItem * newPix = this-> addPixmap(dragPix );
newPix-> setPos(event-> pos()。x(),event-> pos()。y());
}

场景依然不会接受滴。我猜这是因为我不能在我的 QGraphicsScene setAcceptDrops(true)



如何在图形场景中接受滴滴?

解决方案

这里的诀窍是还要接受QGraphicsScene :: dragMoveEvent()!中的事件!



原因是DEFAULT实现,如果鼠标下没有任何项目则忽略拖放事件! / p>

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



干杯


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

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

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