Qt mouseMoveEvent only when left click

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 a temporary preview of the string, and when I release, I draw the actual string. I would like to know how I can make mouseMoveEvent work only when I have the left mouse button pressed. 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 does nothing. Any help would be greatly appreciated

+5
source share
1 answer

From the documentation QMouseEvent::button():

Note that the return value is always Qt :: NoButton for event mouse movement.

Use instead buttons().

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

All Articles