Clicking two mouse buttons with QtTest
-
I want to test that this piece gets hit:
if(elapsedTime < QApplication::doubleClickInterval() && event->buttons().testFlag(Qt::LeftButton) && event->buttons().testFlag(Qt::RightButton)) { if(!isPressed()) { pressIfReleased(); mNeighboursPressed = true; emit pressNeighbours(); } for(QTimer* timer : { &mSingleMouseTimerRight, &mSingleMouseTimerLeft }) { timer->stop(); } return; }
I tryed this but it is not detected as both pressed together:
QTest::mousePress(&obj, Qt::LeftButton, Qt::NoModifier, obj.rect().center()); QTest::mousePress(&obj, Qt::RightButton, Qt::NoModifier, obj.rect().center());
So what can i do ?
-
@sandro4912 said in Clicking two mouse buttons with QtTest:
So what can i do ?
What about QTest::mouseDClick(...)?
-
But that only clicks one button twice and not two buttons simultaneous?
-
@sandro4912 said in Clicking two mouse buttons with QtTest:
that only clicks one button twice and not two buttons simultaneous?
Yes, you're right. Sorry, I was misled by this
QApplication::doubleClickInterval
...Anyway, I guess (not tested) that you can do:
QTest::mousePress(&obj, Qt::LeftButton | Qt::RightButton, Qt::NoModifier, obj.rect().center());
I mean, given that QMouseEvent::buttons() returns
the button state when the event was generated. The button state is a combination of Qt::LeftButton, Qt::RightButton, Qt::MidButton using the OR operator.
you may be able to do the opposite, creating a mouse press event with the two buttons pressed.
-
I'm not sure where this is defined, as it doesn't seem to be a registered framework virtual:
QTest::mousePress(&obj, Qt::LeftButton, Qt::NoModifier, obj.rect().center());
So try overriding the QWidget::mousePressEvent(QMouseEvent* e) virtual and check the state of the buttons as reported in e->buttons().
-
@Pablo-J-Rogina said in Clicking two mouse buttons with QtTest:
QTest::mousePress(&obj, Qt::LeftButton | Qt::RightButton, Qt::NoModifier, obj.rect().center());
Unfortunately it does not compile.
mousePress
semms to take onlybutton
notbuttons
-
@sandro4912 said in Clicking two mouse buttons with QtTest:
Unfortunately it does not compile
I adviced my code was not tested :-)
Anyway, could you please post the compile error?mousePress semms to take only button not buttons
Yes, per method signature
void QTest::mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = ..., QPoint pos = ..., int delay = ...)
so the idea is to combine both Qt::LeftButton which is 1 and Qt::RightButton which is 2 into just one value with OR function -> 3.
What if you try with this (again not tested):
QTest::mousePress(&obj, 3, Qt::NoModifier, obj.rect().center());
-
@Pablo-J-Rogina said in Clicking two mouse buttons with QtTest:
Qt::MouseButton button
That also does not work. I think you cannot combine it with
or
forQt::MouseButton button
only forQt::MouseButtons buttons
.Anyway i realized I have a error in the code I want to test. The test code is okay. I don't really need to press two buttons exactly at the same time.
My code allows to have the buttons clicked with a short delay of 50ms. Then it still counts as clicked together.
Here just for curiosity the code to test:
The press event:
if(!(event->buttons().testFlag(Qt::LeftButton) || event->buttons().testFlag(Qt::RightButton))) { return; } if(event->buttons().testFlag(Qt::LeftButton)) { mSingleMouseTimerLeft.start(); } else if (event->buttons().testFlag(Qt::RightButton)){ mSingleMouseTimerRight.start(); } const auto elapsedTime = mElapsedTimer.restart(); if(elapsedTime >= QApplication::doubleClickInterval()) { return; } if((mSingleMouseTimerLeft.isActive() && event->buttons().testFlag(Qt::RightButton)) || (mSingleMouseTimerRight.isActive() && event->buttons().testFlag(Qt::LeftButton))){ if(!isPressed()) { pressIfReleased(); mNeighboursPressed = true; emit pressNeighbours(); } for(QTimer* timer : { &mSingleMouseTimerRight, &mSingleMouseTimerLeft }) { timer->stop(); } }
In constructor:
constexpr auto intervall = 50; for(QTimer* timer : {&mSingleMouseTimerRight, &mSingleMouseTimerLeft}){ timer->setInterval(intervall); timer->setSingleShot(true); } connect(&mSingleMouseTimerLeft, &QTimer::timeout, this, &Cell::pressIfReleased); connect(&mSingleMouseTimerRight, &QTimer::timeout, this, &Cell::mark);
So in Short if the user clicks Right and Left in less than 50ms after each other it counts as both buttons clicked together.
The test code stays the same as posted
Still i wonder why theres no method which Supports the ``Qt::MouseButtons buttons` to press 2 Buttons like you suggested simultaneously.