Qt mouseMoveEvent only when left mouse button is pressed

倾然丶 夕夏残阳落幕 提交于 2020-01-03 13:00:51

问题


I currently have a program that draws lines and rectangles.

void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);

I use mouseMoveEvent to draw temporary preview of a line and when i release i draw the actual line. What I would like to know is how can i make mouseMoveEvent work work only when i have the left mouse button pressed down. I tried the following but then the whole function stops working.

void mouseMoveEvent(QMouseEvent *event)
{
     if(event->button() == Qt::LeftButton)
     {
        //do stuff
     }
}

but then the function doesn't do anything. Any assistance would be much appreciated


回答1:


From the documentation of QMouseEvent::button():

Note that the returned value is always Qt::NoButton for mouse move events.

You should use buttons() instead.

if(event->buttons() & Qt::LeftButton)
{
    //do stuff
}


来源:https://stackoverflow.com/questions/16279762/qt-mousemoveevent-only-when-left-mouse-button-is-pressed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!