简体   繁体   English

使用 QTest 模拟鼠标移动

[英]Simulation mouse movement using QTest

I'm using QTest to test simple widgets and it all works as expected.我正在使用QTest来测试简单的小部件,并且一切都按预期工作。 But now I have a more complex test scenario, where I have basically a widget that allows the user to press the mouse button, then moves some content, and then releases the mouse button.但是现在我有一个更复杂的测试场景,我基本上有一个小部件,它允许用户按下鼠标按钮,然后移动一些内容,然后释放鼠标按钮。

For this I created the following test harness:为此,我创建了以下测试工具:

main.cpp主程序

#include <QtTest/QTest>
#include "TestObject.h"

int main(int argc, char** args) {
    TestObject o;
    QTest::qExec(&o);

}

WidgetToTest.h WidgetToTest.h

#pragma once
#include <QWidget>

class WidgetToTest : public QWidget
{
    Q_OBJECT
public:
    WidgetToTest(QWidget* parent = nullptr);

protected:
    void mousePressEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
};

WidgetToTest.cpp WidgetToTest.cpp

#include "WidgetToTest.h"
#include <QDebug>
#include <QMouseEvent>
#include <QHBoxLayout>

WidgetToTest::WidgetToTest(QWidget* parent): QWidget(parent)
{
    setFixedSize(200, 200);
    setLayout(new QHBoxLayout);
}

void WidgetToTest::mousePressEvent(QMouseEvent* event)
{
    qDebug() << "Mouse press: " << event;
}

void WidgetToTest::mouseMoveEvent(QMouseEvent* event)
{
    qDebug() << "Mouse move: "<< event; // Nothing happens here???
}

void WidgetToTest::mouseReleaseEvent(QMouseEvent* event)
{
    qDebug() << "Mouse release: " << event;
}

TestObject.h测试对象.h

#pragma once

#include <QObject>
class TestObject : public QObject
{
    Q_OBJECT

private slots:
    void testCode();
};

TestObject.cpp测试对象.cpp

#include "TestObject.h"
#include "WidgetToTest.h"
#include <QTest>
#include <QApplication>

void TestObject::testCode()
{
    int argc{0};
    QApplication app(argc,{});

    Qt::KeyboardModifiers mod;
    auto w = new WidgetToTest;
    w->show();
    QTest::mousePress(w, Qt::MouseButton::LeftButton,mod,QPoint(5,5));
    QTest::mouseMove(w, QPoint(20,20),20);
    QTest::mouseRelease(w, Qt::MouseButton::LeftButton,mod, QPoint(20,20));
}

So the user clicks on position (5,5) inside the widget with the left mouse button and then drags the mouse while holding the button to position (20,20) where he releases the button at position (20,20).因此,用户用鼠标左键单击小部件内的位置 (5,5),然后在按住按钮的同时将鼠标拖动到位置 (20,20),他在位置 (20,20) 处释放按钮。

Interestingly, the move event never occurs inside the widget and I really don't know why.有趣的是,移动事件从未发生在小部件内,我真的不知道为什么。

It seems, that I didn't fully grasped the intention of QTest::mouseMove , but the Qt documentation is also rather taciturn in how to use it.看来,我没有完全QTest::mouseMove的意图,但 Qt 文档在如何使用它方面也相当沉默。

How can I simulate my desired behavior?如何模拟我想要的行为?

The following changes in void TestObject::testCode() are working. void TestObject::testCode()中的以下更改正在起作用。

  1. Enable mouse tracking for the widget.为小部件启用鼠标跟踪。
  2. Add delay to mouse release test event otherwise the mouse move event seems to get lost/late.为鼠标释放测试事件添加延迟,否则鼠标移动事件似乎丢失/延迟。

     auto w = new WidgetToTest; w->setMouseTracking(true); w->show(); QTest::mousePress(w, Qt::MouseButton::LeftButton,mod,QPoint(5,5)); QTest::mouseMove(w, QPoint(20,20),20); QTest::mouseRelease(w, Qt::MouseButton::LeftButton,mod, QPoint(20,20),20);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM