Qt how to check which mouse button is pressed

I have problems in PySide, trying to determine which mouse button is clicked in an event function. I need this, in particular, to ignore the mouse move event, because it does the work on both mouse buttons, left and right.

I want to ignore the mouse move event if the right button on the stage is pressed. Any help?

+6
source share
4 answers

All mouse events have two methods ( button and buttons ) to determine which button is pressed. But only in the case of move , the documentation says:

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

for mouseMoveEvent you must use the buttons method.

 void mouseMoveEvent(QMouseEvent *e) { if(e->buttons() == Qt::RightButton) qDebug() << "Only right button"; } 

To ignore move events, you need to do this work in eventFilter , of course.

+14
source

QApplication::mouseButtons() will return the status of mouseButton , so you can get the status of the mouse in KeyPressEvent .

+6
source

You can use a boolean value:

 void mousePressEvent(QMouseEvent *event) { if (event->button()==Qt::RightButton){ qDebug() << "right button is pressed pressed=true; //<----- } } 

and on mouseMoveEvent

 void GLWidget::mouseMoveEvent(QMouseEvent *event) { float dx = event->x() - lastPos.x(); // where lastpos is a QPoint member float dy = event->y() - lastPos.y(); if (dx<0) dx=-dx; if (dy<0) dy=-dy; if (event->buttons() & Qt::LeftButton) { //if you have MOVEd ...do something } if (event->buttons() & Qt::RightButton) { if (pressed==true) return; else{ ...do } } } 

On mouserelease you should set pressed = false; ("pressed" must be a member of the class)

Hope this helps let me know

+2
source

you can check which mouse button is pressed through Qt::RightButton . Sorry for the C ++ code, but I hope you understand the idea anyway:

 void mousePressEvent(QMouseEvent *event) { if (event->button()==Qt::RightButton){ qDebug() << "right button is pressed } } 
+1
source

Source: https://habr.com/ru/post/945870/


All Articles