如何使用QTestLib模拟鼠标滚轮事件[Qt5] [英] How to simulate mouse wheel events using QTestLib [Qt5]

查看:33
本文介绍了如何使用QTestLib模拟鼠标滚轮事件[Qt5]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴使用QTestLib为我的Qt5基于UI的小部件编写测试。直到现在,当我试图找到一种方法来模拟鼠标滚轮事件时,它似乎并不缺少特性和方便的功能。

我看过official documentationofficial example,但似乎想不出如何开始模拟鼠标滚轮事件。

这不存在吗?或者我错过了什么?我应该如何使用QTestLib创建虚拟鼠标滚轮事件?

推荐答案

对于10年后遇到此问题的任何人。我们为我们的Qt4.8测试套件编写了一个实现(也是在Qt5.6中测试的,一如既往不能保证):

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

这篇关于如何使用QTestLib模拟鼠标滚轮事件[Qt5]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆