c++ - 停止从 QGraphicsItem 到 QGraphicsScene QT 的事件传播

标签 c++ qt events

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

这是我的 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());
    }

}

最佳答案

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

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

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

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

关于c++ - 停止从 QGraphicsItem 到 QGraphicsScene QT 的事件传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12991807/

相关文章:

c++ - 求一个boost::bind()的限制的例子

QTableView 添加到设计器 : not showing data

linux - QXcbConnection : Could not connect to display Aborted, 在linux上安装QT时

c++ - Qt:如何通过外部程序打开文件, "open with..."对话框

events - 如何触发附加到 ZF2 中共享事件管理器的事件?

javascript - 异步计时器事件在 Firefox 4 中同步运行 ("buggy")?

c# - 在 .NET 中如何检查事件是否已被订阅?

c++ - C++ 中按钮的简单信号

c++ - 带有成员 QWidget 和 QLayout 的 QMainWindow 在退出时崩溃,如何解决?

c++ - 为什么构造函数不能推断模板参数?