【问题标题】:stop event propagation from QGraphicsItem to QGraphicsScene QT停止从 QGraphicsItem 到 QGraphicsScene QT 的事件传播
【发布时间】:2013-03-07 22:05:24
【问题描述】:

我有一个 Qgraphicsscene 实现为一个类,然后我使用 QGraphicsScene::mousePressEvent 添加一个 QGraphicsRectItem,这个项目也有一个 QGraphicsRectItem::mousePressEvent 的实现,问题是矩形项目中的事件被传播到场景,当我点击它时添加了一个新的矩形项目,但我想要的是这个项目内的事件不会传播到场景,我尝试事件->接受但事件被传播,我怎么不能这样做?感谢您的帮助。

这是我的 qgraphicsscene 代码:

#include "imageview.h"

ImageView::ImageView(QWidget *parent){
    scene = new ImageScene(this);
    setScene(scene);
    //this->setMouseTracking(true);
    this->setInteractive(true);
}


ImageScene::ImageScene(QWidget *parent){
    current = NULL;
    selection = new QRubberBand(QRubberBand::Rectangle,parent);
    selection->setGeometry(QRect(10,10,20,20));
    setSceneRect(0,0,500,500);
}

void ImageScene::mousePressEvent(QGraphicsSceneMouseEvent *event){
    QGraphicsScene::mousePressEvent(event);
    /*IGNORING THIS EVENT FROM QGRAPHICSRECTITEM*/
    cout<<"image view"<<endl;
    if(this->selectedItems().length() == 0){ /*WORKS BUT IN SOME IMPLEMENTATION IS A PROBLEM (WHEN I DELETE THE ITEM WITH A DELETE BUTTON THE EVENT IS FIRED AND ADD A NEW ITEM .)*/
        origin = event->scenePos();
        selection->show();
    }
}
void ImageScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    if(selection->isVisible() && selection->rect().width() >= 20 && selection->rect().height() >= 20){
        QGraphicsScene::mouseReleaseEvent(event);

        ResizableRect * rselection = new ResizableRect();
            //selection->origin = event->scenePos();
            //selection->grabMouse();
        cout<<"add"<<endl;
        this->addItem(rselection);
        rselection->setPos(selection->pos());
        rselection->setRect(0,0,selection->rect().width(),selection->rect().height());
    }
    selection->hide();

}
void ImageScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
    QGraphicsScene::mouseMoveEvent(event);
    if(selection->isVisible()){
        QPoint rorigin(origin.x(),origin.y());
        int xdes = event->scenePos().x();
        int ydes = event->scenePos().y();
        xdes = xdes > 0? xdes:0;
        ydes = ydes > 0? ydes:0;
        xdes = xdes < this->width()?xdes:this->width();
        ydes = ydes < this->height()?ydes:this->height();

        QPoint rdest(xdes,ydes);
        selection->setGeometry(QRect(rorigin,rdest).normalized());
    }

}

【问题讨论】:

  • 您的代码中对 event->accept() 的调用在哪里?
  • ResizableRect类的mousePressEvent函数中,该类扩展了QGraphicsRectItem。
  • herehere。希望对您有所帮助。
  • 感谢您的帮助,我按照 satuon 的说明阅读了文档,并使用 mouseReleaseEvent 解决了问题(不是我想要的,但可以正常工作),但最初的事故仍然存在,请打开此线程以查看谁能确定为什么 qGraphicsitem 中的 event->accept 不会停止传播到 qGraphicsScene,可能是 Qt 中的错误?

标签: c++ qt events


【解决方案1】:

与 QWidgets 相对,QGraphicsScene 在子项之前捕获事件。它在 Qt 文档中有所描述。

为了正确使用它,请使用 QGraphicsView 的重新实现而不是 QGraphcisScene。 在那里重新实现 mousePressEvent。

此时您可以确定鼠标指针下的项目。 它就在那里 - 你可以调用 QGraphicsView::mousePressEvent(); 它不是 - 使用您的实现来添加新项目。

它还允许您分离不同视图的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 2012-10-10
    • 2011-04-06
    相关资源
    最近更新 更多