简体   繁体   English

跳过qgraphics场景中qgraphicsitem的mouseEvent

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

I know how to pass events from qgraphics scene to q graphics item ,but the problem is for the item,the mouse events for the scene is being executed. 我知道如何将事件从qgraphics场景传递到q图形项,但是问题出在项目上,正在执行场景的鼠标事件。

for example in the code below when pressing on the item the output is "custom scene is pressed" 例如在下面的代码中,当按下该项目时,输出为“按下了自定义场景”

 #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();
}

See the documentation of QGraphicsSceneMouseEvent::pos : 请参阅QGraphicsSceneMouseEvent :: pos的文档:

Returns the mouse cursor position in item coordinates. 返回项目坐标中的鼠标光标位置。

This means if the mouse is 10 pixels away from the top and left border of your item, you will get (10,10) as coordinates no matter where on the scene the item is. 这意味着,如果鼠标距离项目的顶部和左侧边界10像素,则无论项目在场景中的什么位置,您都将获得(10,10)作为坐标。

What you need is QGraphicsSceneMouseEvent::scenePos : 您需要的是QGraphicsSceneMouseEvent :: scenePos

Returns the mouse cursor position in scene coordinates. 返回场景坐标中的鼠标光标位置。

Change your if -statement to: 将您的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