QT Application Programming: Solve the problem that QGraphicsScene cannot get the mouse coordinates directly when rewriting mouse events

1. Environmental introduction

Operating system introduction: win10 64 bit

QT version:  5.12.6

2. Phenomenon

Overload the mouse events related to QGraphicsScene. If you want to get the coordinates of the current mouse, if you get the coordinates directly from mouseEvent, the returned coordinates are all 0.

class my_graphicsScene : public QGraphicsScene
{
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
}

......
void my_graphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    //鼠标左键按下
    if(mouseEvent->button()==Qt::LeftButton)
    {
        //记录鼠标按下的点
        start_point=mouseEvent->pos(); //相对窗口坐标---->这里得到的坐标全是0
    }
}

Three, the solution

/*
工程: ECRS
日期: 2021-01-02
作者: DS小龙哥
环境: win10 QT5.12.6 MinGW32
功能: 鼠标移动事件
*/
void my_graphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    QGraphicsScene::mousePressEvent(mouseEvent);
    qDebug()<<"坐标:"<<mouseEvent->scenePos();
}

 

The final effect of the program is that the video screen is partially enlarged, and the mouse frame is used to complete it in a certain area. 

 

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/112130645