View Full Version : How to generate an event for "both the mouse buttons clicked together"?
seguprasad
20th November 2007, 04: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.
Thanks,
Segu
babu198649
20th November 2007, 05: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
wysota
20th November 2007, 07:18
No, it won't work. button() will return only one of the buttons. Using QMouseEvent::buttons() seems better though.
babu198649
20th November 2007, 10: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
}
}
wysota
20th November 2007, 10:24
I don't know what you mean :) But your code is faulty - you are missing one equality sign ('=') before Qt::RightButton.
babu198649
20th November 2007, 10:38
ok thanks for pointing out error i will try and will post my result i have edited the bug in the previous post;).
wysota
20th November 2007, 10: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))
doSomething();
else
baseClass::mousePressEvent(e);
}
One might also use == instead of & depending on the effect one wants to obtain.
babu198649
20th November 2007, 12: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))
code();
i have tested it and it works
but one question why do we use or operator and that too unary.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.