10

I have problems in PySide while trying to determine which mouse button is pressed in event function. I need it in particular for ignoring mouse move event, because it's doing job on both mouse buttons, left and right.

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

Alex
  • 3,167
  • 6
  • 35
  • 50

4 Answers4

22

All of mouse events have two methods (button and buttons) to determine which of buttons are pressed. But for only move event the documentation says:

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

for mouseMoveEvent you should use buttons method.

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

In order to ignore move events you need to do this work in eventFilter of course.

fasked
  • 3,555
  • 1
  • 19
  • 36
  • That's what I need. Thank you so much. I would open new question for eventFilter if you can help me with mouse filtering. – Alex May 27 '13 at 10:07
  • @Alex you're welcome, I will see your question about `eventFilter` as soon as possible, but I am able to provide you C++ code only. – fasked May 27 '13 at 10:40
  • Posted HERE: http://stackoverflow.com/questions/16772227/qt-mouse-event-propagation-with-scene-items – Alex May 27 '13 at 11:31
14

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

Tom Gringauz
  • 801
  • 8
  • 16
ken
  • 199
  • 1
  • 11
  • To me it seems that this answer doesn't match OPs question well. Nevertheless, it answers perfectly my question for which I have just googled and found this Q/A. :-) – Scheff's Cat Apr 19 '22 at 15:46
2

you can check, which mouse button is pressed via Qt::RightButton. Sorry for c++ code, but i hope, you would understand idea anyway:

void mousePressEvent(QMouseEvent *event)
{ 
    if (event->button()==Qt::RightButton){
        qDebug() << "right button is pressed
    }
}
Shf
  • 3,463
  • 2
  • 26
  • 42
  • 1
    Vote up for your engagement, but I already tried this, and while it works with mouse press and mouse release, it not works in MOUSE MOVE event. – Alex May 26 '13 at 14:24
  • This doesn't work for me. The widget receives `QContextMenuEvent` instead of `QMouseEvent` no matter what I do - even though I have no menu, and trying to use `setContextMenuPolicy` doesn't change a thing. – rr- May 16 '15 at 12:59
  • You have to use `.buttons()` (note the 's') on mouseMoveEvents as `.button()` will always return Qt::NoButton for this event. https://doc.qt.io/qt-5/qmouseevent.html#button – user136036 Feb 28 '21 at 20:08
2

You could use a boolean:

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 have to set pressed=false; ( "pressed" must be a member of the class)

Hope it helps,let me know

Tcz
  • 661
  • 5
  • 18
  • +1 for try, but it's not what I need. I wouldn't put boolean for that matter, too risky when user is clumsy with mouse...have noticed that Qt is not the very best with mouse event processing. I am assured that there's built-in method for that purpose. – Alex May 26 '13 at 15:52
  • This doesn't seem to work properly, it only seemed to pickup the mouse button if the statement read: `if (event->buttons() == Qt::LeftButton)` for example, rather than `button()`. – EM-Creations Jan 31 '18 at 10:19