How to simulate mouse wheel events using QTestLib [Qt5]
Asked Answered
C

2

6

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?

Crossly answered 23/6, 2018 at 0:36 Comment(1)
I found this 10 year old post on a forum, I really hope this was added since then: qtcentre.org/threads/13436-Wheel-and-QTestCrossly
P
2

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);
}
Parabola answered 13/7, 2021 at 8:8 Comment(2)
Bro, we are in the decades club xDCrossly
Yee, imagine us sitting here in 10 years, sipping our cyberwhisky and watching some newbie requesting this feature for Qt420, telling him: in our time when we came up with this solution, we had to walk to our workstation to code, uphill, in both directions. ;D On a more serious note: I wonder if and when this will be implemented. Qt just aquired froglogic so maybe QTest will be obsolete soon for a more convoluted, complicated and expensive environment. Looking forward to it.Parabola
C
4

It has been 10 years, and I thought it would be a nice way to mark the occasion by formally submitting a feature request on the bug-tracker for Qt:

Behold QTBUG-71449.

Crossly answered 27/10, 2018 at 21:42 Comment(0)
P
2

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);
}
Parabola answered 13/7, 2021 at 8:8 Comment(2)
Bro, we are in the decades club xDCrossly
Yee, imagine us sitting here in 10 years, sipping our cyberwhisky and watching some newbie requesting this feature for Qt420, telling him: in our time when we came up with this solution, we had to walk to our workstation to code, uphill, in both directions. ;D On a more serious note: I wonder if and when this will be implemented. Qt just aquired froglogic so maybe QTest will be obsolete soon for a more convoluted, complicated and expensive environment. Looking forward to it.Parabola

© 2022 - 2024 — McMap. All rights reserved.