View Full Version : How to generate an event for "both the mouse buttons clicked together"?

20th November 2007, 05:02
Hi All,

I have a requirement like following,
I need to create a box by clicking both the mouse buttons together, hold and drag should create a box on the plot.

Can you please provide me the solution that I can catch the event for "both the mouse buttons clicked to gether" in Qt? I am not able find this in QEvent or QMouseEvent.


20th November 2007, 06:35
//QMouseEvent * mouseEvent
if( (mouseEvent->button() == Qt::LeftButton) &&
(mouseEvent->button() =Qt::RightButton))
//your code

also see this function
Qt::MouseButtons QMouseEvent::buttons () const

Hope this helps

20th November 2007, 08:18
No, it won't work. button() will return only one of the buttons. Using QMouseEvent::buttons() seems better though.

20th November 2007, 11:10
the right click followed by leftclick(vice-versa) where the former is still active .does this not work

in other words

if( (mouseEvent->button() == Qt::LeftButton)
if(mouseEvent->button() == Qt::RightButton))
//your code

20th November 2007, 11:24
I don't know what you mean :) But your code is faulty - you are missing one equality sign ('=') before Qt::RightButton.

20th November 2007, 11:38
ok thanks for pointing out error i will try and will post my result i have edited the bug in the previous post;).

20th November 2007, 11:52
To me the proper way of doing what the thread author wants is:

void XXX::mousePressEvent(QMouseEvent *e){
if(e->buttons() & (Qt::LeftButton|Qt::RightButton))

One might also use == instead of & depending on the effect one wants to obtain.

20th November 2007, 13:15
i tried u r code it enters if condition even if one button is clicked .but the author of post wants only when both the buttons are active.
this can be done by

if(event->buttons() == (Qt::LeftButton | Qt::RightButton))

i have tested it and it works

but one question why do we use or operator and that too unary.