Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtestsupport_core.h
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QTESTSUPPORT_CORE_H
5#define QTESTSUPPORT_CORE_H
6
7#include <QtCore/qcoreapplication.h>
8#include <QtCore/qdeadlinetimer.h>
9
10#include <chrono>
11
13
14namespace QTest {
15
16Q_CORE_EXPORT void qSleep(int ms);
17Q_CORE_EXPORT void qSleep(std::chrono::milliseconds msecs);
18
19template <typename Functor>
20[[nodiscard]] bool
22{
23 // We should not spin the event loop in case the predicate is already true,
24 // otherwise we might send new events that invalidate the predicate.
25 if (predicate())
26 return true;
27
28 // qWait() is expected to spin the event loop at least once, even when
29 // called with a small timeout like 1ns.
30
31 do {
32 // We explicitly do not pass the remaining time to processEvents, as
33 // that would keep spinning processEvents for the whole duration if
34 // new events were posted as part of processing events, and we need
35 // to return back to this function to check the predicate between
36 // each pass of processEvents. Our own timer will take care of the
37 // timeout.
40
41 if (predicate())
42 return true;
43
44 using namespace std::chrono;
45
46 if (const auto remaining = deadline.remainingTimeAsDuration(); remaining > 0ns)
47 qSleep((std::min)(10ms, ceil<milliseconds>(remaining)));
48
49 } while (!deadline.hasExpired());
50
51 return predicate(); // Last chance
52}
53
54template <typename Functor>
59
60Q_CORE_EXPORT void qWait(int ms);
61
62Q_CORE_EXPORT void qWait(std::chrono::milliseconds msecs);
63
64} // namespace QTest
65
67
68#endif
static void processEvents(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
Processes some pending events for the calling thread according to the specified flags.
static void sendPostedEvents(QObject *receiver=nullptr, int event_type=0)
Immediately dispatches all events which have been previously queued with QCoreApplication::postEvent(...
\inmodule QtCore
bool hasExpired() const noexcept
Returns true if this QDeadlineTimer object has expired, false if there remains time left.
std::chrono::nanoseconds remainingTimeAsDuration() const noexcept
Returns the time remaining before the deadline.
@ DeferredDelete
Definition qcoreevent.h:100
const auto predicate
Combined button and popup list for selecting options.
bool qWaitFor(Functor predicate, QDeadlineTimer deadline=QDeadlineTimer(std::chrono::seconds{5}))
Q_CORE_EXPORT void qSleep(int ms)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Q_CORE_EXPORT void qWait(int ms)
This is an overloaded member function, provided for convenience. It differs from the above function o...
@ PreciseTimer
GLbitfield GLuint64 timeout
[4]
QDeadlineTimer deadline(30s)