QTestEventLoop Class
The QTestEventLoop class runs an event loop in a test, with a timeout. More…
| Header | #include <QTestEventLoop> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Test) |
| Module | Nt Test C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QTestEventLoop(QObject *parent = nullptr) | |
| ~QTestEventLoop() | |
| void | enterLoop(int seconds) |
| void | enterLoopMSecs(int milliseconds) |
| void | enterLoop(std::chrono::milliseconds timeout) |
| void | changeInterval(int milliseconds) |
| bool | timeout() const |
Public Slots
| void | exitLoop() |
Static Public Members
| QTestEventLoop & | instance() |
Reimplemented Protected Functions
| void | timerEvent(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
QTestEventLoop::QTestEventLoop(QObject *parent = nullptr)
Constructs an event loop object with the given parent.
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.
void QTestEventLoop::exitLoop()
Ends the running loop, if there is one.
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.
void QTestEventLoop::timerEvent(QTimerEvent *event)
Reimplements QObject::timerEvent().
Ends the loop when the timeout of event elapses.