我很高兴使用QTestLib为基于Qt5小部件的UI编写测试。直到现在,在我试图找到一种模拟鼠标轮事件的方法之前,似乎还不缺少功能和方便功能。
我看过正式文件和一个官方例子,但我似乎不知道如何模拟鼠标轮事件。
这不存在吗?还是我遗漏了什么?如何使用创建虚拟鼠标轮事件?
发布于 2021-07-13 08:08:29
对于任何从现在起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);
}发布于 2018-10-27 21:42:03
这已经过去10年了,我认为这将是一个很好的方式来纪念这一时刻,在Qt的bug跟踪器上正式提交一个特性请求:
看看QTBUG-71449。
https://stackoverflow.com/questions/50996997
复制相似问题