Mr. Developerdude
Mr. Developerdude

Reputation: 9648

How to simulate mouse wheel events using QTestLib [Qt5]

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?

Upvotes: 7

Views: 1214

Answers (2)

Baumflaum
Baumflaum

Reputation: 1385

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);
}

Upvotes: 3

Mr. Developerdude
Mr. Developerdude

Reputation: 9648

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.

Upvotes: 5

Related Questions