6.0.0-rc4Source

QTestEventLoop Class

The QTestEventLoop class runs an event loop in a test, with a timeout. More…

Header#include <QTestEventLoop>
CMaketarget_link_libraries(app PRIVATE Nt::Test)
ModuleNt Test C++ Classes
InheritsQObject

List of all members, including inherited members

Public Functions

QTestEventLoop(QObject *parent = nullptr)
~QTestEventLoop()
voidenterLoop(int seconds)
voidenterLoopMSecs(int milliseconds)
voidenterLoop(std::chrono::milliseconds timeout)
voidchangeInterval(int milliseconds)
booltimeout() const

Public Slots

voidexitLoop()

Static Public Members

QTestEventLoop &instance()

Reimplemented Protected Functions

voidtimerEvent(QTimerEvent *event)

Detailed Description

Use it to wait for something asynchronous when QTest::qWaitFor() or QSignalSpy::wait() do not fit. Connect the event to exitLoop(), enter the loop, and check timeout():

QTestEventLoop &loop = QTestEventLoop::instance();
QObject::connect(&socket, &QTcpSocket::connected, &loop, &QTestEventLoop::exitLoop);
socket.connectToHost(QHostAddress::LocalHost, port);
loop.enterLoop(5);
QVERIFY(!loop.timeout());

Differences from Qt

enterLoopMSecs() and changeInterval() take an int number of milliseconds. There is no aboutToEnter handling of test failures.

Member Function Documentation

[explicit] QTestEventLoop::QTestEventLoop(QObject *parent = nullptr)

Constructs an event loop object with the given parent.

[override] QTestEventLoop::~QTestEventLoop()

Destroys the event loop object.

void QTestEventLoop::changeInterval(int milliseconds)

Restarts the timeout of the running loop, with milliseconds milliseconds left. It does nothing if the loop is not running.

void QTestEventLoop::enterLoop(int seconds)

Runs the event loop until exitLoop() is called, or for at most seconds seconds.

void QTestEventLoop::enterLoop(std::chrono::milliseconds timeout)

This is an overloaded function.

Runs the event loop for at most timeout.

void QTestEventLoop::enterLoopMSecs(int milliseconds)

Runs the event loop until exitLoop() is called, or for at most milliseconds milliseconds. Afterwards timeout() tells which it was.

[slot] void QTestEventLoop::exitLoop()

Ends the running loop, if there is one.

[static] QTestEventLoop & QTestEventLoop::instance()

Returns the shared event loop object.

bool QTestEventLoop::timeout() const

Returns true if the last run of the loop ended because of its timeout.

[override, protected] void QTestEventLoop::timerEvent(QTimerEvent *event)

Reimplements QObject::timerEvent().

Ends the loop when the timeout of event elapses.