3

I'm using QTest to test simple widgets and it all works as expected. 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

#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

#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

#pragma once

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

private slots:
    void testCode();
};

TestObject.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).

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.

How can I simulate my desired behavior?

Aleph0
  • 5,816
  • 4
  • 29
  • 80

1 Answers1

1

The following changes in void TestObject::testCode() are working.

  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);
    
Mathias Schmid
  • 431
  • 4
  • 7
  • I think that is not the solution. If you use a widget you will see that only mouseMoveEvent is called if the mouse presses the widget, when activating the flag mouseTracking you enable that it is only necessary that the cursor be over the widget (it is not necessary to press the widget, it would be enough with a hover event ), and I think the OP does not want to test it. For me it is a bug. – eyllanesc Jun 24 '19 at 20:37