I am happily using QTestLib
to write tests for my Qt5
widgets based UI. There has seemed to be no shortage of features and convenience functionality until now, when I tried to find a way to simulate mouse wheel events.
I have looked at the official documentation, and an official example but I can't seem to figure out how to go about simulating mouse wheel events.
Does this not exist? Or am I missing something? How should I create dummy mouse wheel events using QTestLib
?
For anyone stumbling into this problem 10 years from now on. We wrote an implementation for our testing suite for Qt4.8 (also tested in Qt5.6, as always no guarantees):
void TestUtility::mouseWheelTurn(
QWidget *widget, // The most top level widget; a MainWindow in our case
int delta, // As in QWheelEvent
QPoint pos, // Mouseposition in the moment of scrolling relative to top level widget
Qt::MouseButtons buttons, // As in QWheelEvent
Qt::KeyboardModifiers modifiers, // As in QWheelEvent
Qt::Orientation orientation, // As in QWheelEvent
int delay) // As in other QTest functions
{
QWidget *toWheelChild = widget->childAt(pos);
if(toWheelChild == NULL) return;
pos = widget->mapToGlobal(pos);
pos = toWheelChild->mapFromGlobal(pos);
QTest::mouseMove(toWheelChild, pos);
QTest::qWait(delay);
QWheelEvent *wheelEvent =
new QWheelEvent(pos, delta, buttons, modifiers, orientation);
QApplication::instance()->postEvent(toWheelChild, wheelEvent);
}