繁体   English   中英

跳过qgraphics场景中qgraphicsitem的mouseEvent

[英]skipping the mouseEvents of the qgraphicsitem in a qgraphics scene

我知道如何将事件从qgraphics场景传递到q图形项,但是问题出在项目上,正在执行场景的鼠标事件。

例如在下面的代码中,当按下该项目时,输出为“按下了自定义场景”

 #include <QtGui>
class CustomScene : public QGraphicsScene
{
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        if(itemAt(event->pos()))
            QGraphicsScene::mousePressEvent((event));
        else
        qDebug() << "Custom scene clicked.";
    }
};
class CustomItem : public QGraphicsRectItem
{
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        qDebug() << "Custom item clicked.";
    }
};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CustomItem item;
    item.setRect(20, 20, 60, 60);
    CustomScene scene;
    //scene().set
    scene.addItem(&item);
    QGraphicsView view;
    view.setScene(&scene);
    view.show();
    return a.exec();
}

请参阅QGraphicsSceneMouseEvent :: pos的文档:

返回项目坐标中的鼠标光标位置。

这意味着,如果鼠标距离项目的顶部和左侧边界10像素,则无论项目在场景中的什么位置,您都将获得(10,10)作为坐标。

您需要的是QGraphicsSceneMouseEvent :: scenePos

返回场景坐标中的鼠标光标位置。

将您的if -statement更改为:

 if(itemAt(event->scenePos()))
    QGraphicsScene::mousePressEvent((event));
 else
    qDebug() << "Custom scene clicked.";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM