Distinguish between one and two mouse buttons pressed.
-
I have the following code:
void Cell::mousePressEvent(QMouseEvent *event) { if(event->buttons() & Qt::LeftButton && event->buttons() & Qt::RightButton) { qDebug() << "both pressed"; return; } //.... if(event->buttons() & Qt::LeftButton && !(event->buttons() & Qt::RightButton)) { qDebug() << "left pressed"; } else if(event->buttons() & Qt::RightButton && !(event->buttons() & Qt::LeftButton)) { qDebug() << "right pressed"; } }
I want to detect if both mouse buttons are pressed at the same time or only left or right is pressed.
However when i press both i get
"left pressed" "both pressed"
or
"right pressed" "both pressed"
Do i have to delay the evaluation of right and left with some way so that only both can get detected?
Also i want to check if both mouse buttons are released to trigger an action. This also does not seem to work well:
void Cell::mouseReleaseEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton && event->button() == Qt::RightButton) { qDebug() << "both released"; } }
-
I have the following code:
void Cell::mousePressEvent(QMouseEvent *event) { if(event->buttons() & Qt::LeftButton && event->buttons() & Qt::RightButton) { qDebug() << "both pressed"; return; } //.... if(event->buttons() & Qt::LeftButton && !(event->buttons() & Qt::RightButton)) { qDebug() << "left pressed"; } else if(event->buttons() & Qt::RightButton && !(event->buttons() & Qt::LeftButton)) { qDebug() << "right pressed"; } }
I want to detect if both mouse buttons are pressed at the same time or only left or right is pressed.
However when i press both i get
"left pressed" "both pressed"
or
"right pressed" "both pressed"
Do i have to delay the evaluation of right and left with some way so that only both can get detected?
Also i want to check if both mouse buttons are released to trigger an action. This also does not seem to work well:
void Cell::mouseReleaseEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton && event->button() == Qt::RightButton) { qDebug() << "both released"; } }
@sandro4912 said in Distinguish between one and two mouse buttons pressed.:
Do i have to delay the evaluation of right and left with some way so that only both can get detected?
Yes,
QApplication::doubleClickInterval()
holds the value of that delay in ms. create aQElapsedTimer
, start it in the constructor then everymousePressEvent
you can check:if( event->buttons() & (Qt::LeftButton | Qt::RightButton) && elapsedTimer.restart() < QApplication::doubleClickInterval()){ if(event->buttons() & Qt::LeftButton && event->buttons() & Qt::RightButton) qDebug() << "both pressed"; }
Also i want to check if both mouse buttons are released to trigger an action.
Depends what you mean by this, but if you mean released at the same time then the previous applies
-
i tryed this:
In the constructor:
mElapsedTimer.start();
void Cell::mousePressEvent(QMouseEvent *event) { if( event->buttons() & (Qt::LeftButton | Qt::RightButton) && mElapsedTimer.restart() < QApplication::doubleClickInterval()){ if(event->buttons() & Qt::LeftButton && event->buttons() & Qt::RightButton) { qDebug() << "both pressed"; return; } } if(event->buttons() & Qt::LeftButton && !(event->buttons() & Qt::RightButton)) { qDebug() << "left pressed"; } else if(event->buttons() & Qt::RightButton && !(event->buttons() & Qt::LeftButton)) { qDebug() << "right pressed"; } }
Still it detects both pressed and left or right
-
That's correct, the idea is that if both buttons get clicked within a short timeframe the first if is triggered. What you want instead is probably something like this
in header
private: QElaplsedTimer mElapsedTimer; QTimer singleMouseTimerLeft; QTimer singleMouseTimerRight;
in constructor:
mElapsedTimer.start(); for(QTimer* timer : {&singleMouseTimerRight,&singleMouseTimerLeft}){ timer->setInterval(QApplication::doubleClickInterval()); timer->setSingleShot(true); } connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "right pressed, no double clicks"}); connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "left pressed, no double clicks"});
void Cell::mousePressEvent(QMouseEvent *event) { if(!(event->buttons() & (Qt::LeftButton | Qt::RightButton))) return; //some other button was pressed const auto elapsedTime = mElapsedTimer.restart(); if(elapsedTime < QApplication::doubleClickInterval()){ if(event->buttons() & Qt::LeftButton && event->buttons() & Qt::RightButton) qDebug() << "both pressed"; for(QTimer* timer : {&singleMouseTimerRight,&singleMouseTimerLeft}) timer->stop(); return; } if(event->buttons() & Qt::LeftButton) singleMouseTimerLeft.start(); else singleMouseTimerRight.start(); }
-
That's correct, the idea is that if both buttons get clicked within a short timeframe the first if is triggered. What you want instead is probably something like this
in header
private: QElaplsedTimer mElapsedTimer; QTimer singleMouseTimerLeft; QTimer singleMouseTimerRight;
in constructor:
mElapsedTimer.start(); for(QTimer* timer : {&singleMouseTimerRight,&singleMouseTimerLeft}){ timer->setInterval(QApplication::doubleClickInterval()); timer->setSingleShot(true); } connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "right pressed, no double clicks"}); connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "left pressed, no double clicks"});
void Cell::mousePressEvent(QMouseEvent *event) { if(!(event->buttons() & (Qt::LeftButton | Qt::RightButton))) return; //some other button was pressed const auto elapsedTime = mElapsedTimer.restart(); if(elapsedTime < QApplication::doubleClickInterval()){ if(event->buttons() & Qt::LeftButton && event->buttons() & Qt::RightButton) qDebug() << "both pressed"; for(QTimer* timer : {&singleMouseTimerRight,&singleMouseTimerLeft}) timer->stop(); return; } if(event->buttons() & Qt::LeftButton) singleMouseTimerLeft.start(); else singleMouseTimerRight.start(); }
That works. So it means I evaluate right and left clicks in a slot called every time the right or left timer runs out?
Then in
mousePressEvent
only both buttons pressed is detected.PS:
You have typos in these lines:
connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "right pressed, no double clicks"}); connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "left pressed, no double clicks"});
It should be:
connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "right pressed, no double clicks";}); connect(&singleMouseTimerLeft,&QTimer::timeout,this,[]()->void{qDebug() << "left pressed, no double clicks";});