QEventLoop Class
The QEventLoop class provides a means of entering and leaving an event loop. More…
| Header | #include <QEventLoop> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Types
| enum | ProcessEventsFlag |
| typedef | ProcessEventsFlags |
Public Functions
| QEventLoop(QObject *parent = nullptr) | |
| ~QEventLoop() | |
| bool | processEvents(ProcessEventsFlags flags = AllEvents) |
| void | processEvents(ProcessEventsFlags flags, int maxTime) |
| void | processEvents(ProcessEventsFlags flags, QDeadlineTimer deadline) |
| int | exec(ProcessEventsFlags flags = AllEvents) |
| void | exit(int returnCode = 0) |
| bool | isRunning() const |
| void | wakeUp() |
Public Slots
| void | quit() |
Detailed Description
An event loop dispatches timers, posted events and socket notifiers. Call exec() to run it until quit() or exit() is called, for example from a slot:
QEventLoop loop;
QTimer::singleShot(1000, [&loop] { loop.quit(); });
loop.exec();
See also QCoreApplication, QTimer and QSocketNotifier.
Differences from Qt
ExcludeUserInputEvents and ExcludeSocketNotifiers are accepted but ignored. A loop always runs the event dispatcher of the thread that calls exec() or processEvents(), and that dispatcher cannot be replaced.
Member Type Documentation
enum QEventLoop::ProcessEventsFlag
This enum controls the types of events processed by processEvents().
| Constant | Description |
|---|---|
QEventLoop::AllEvents | All pending events are processed. |
QEventLoop::ExcludeUserInputEvents | Accepted for compatibility; it has no effect because Nt has no user input events. |
QEventLoop::ExcludeSocketNotifiers | Accepted for compatibility; it has no effect, socket notifiers are always processed. |
QEventLoop::WaitForMoreEvents | Block until an event is available instead of returning immediately. |
typedef QEventLoop::ProcessEventsFlags
The ProcessEventsFlags type is a typedef for QFlags<ProcessEventsFlag>. It stores an OR combination of ProcessEventsFlag values.
Member Function Documentation
QEventLoop::QEventLoop(QObject *parent = nullptr)
Constructs an event loop with the given parent.
QEventLoop::~QEventLoop()
Destroys the event loop.
int QEventLoop::exec(ProcessEventsFlags flags = AllEvents)
Enters the event loop and runs it until exit() or quit() is called, then returns the code passed to exit(). flags is accepted for compatibility and ignored. Returns -1 immediately if the loop is already running.
See also exit(), quit() and isRunning().
void QEventLoop::exit(int returnCode = 0)
Tells the event loop to exit with the return code returnCode, which exec() then returns. It is safe to call from any thread.
bool QEventLoop::isRunning() const
Returns true if the event loop is running in exec(); otherwise returns false.
bool QEventLoop::processEvents(ProcessEventsFlags flags = AllEvents)
Processes pending events once, according to flags, and returns true if any event, timer or notifier was handled. With WaitForMoreEvents it blocks until something is ready.
See also exec().
void QEventLoop::processEvents(ProcessEventsFlags flags, int maxTime)
This is an overloaded function.
Processes the pending events for at most maxTime milliseconds, or until there are none left, as flags selects. It does not wait for new events, even with WaitForMoreEvents. Call it during a long computation to keep the program responsive.
See also exec().
void QEventLoop::processEvents(ProcessEventsFlags flags, QDeadlineTimer deadline)
This is an overloaded function.
Processes the pending events until deadline expires or there are none left, as flags selects.
This function was introduced in 6.7.
void QEventLoop::quit()
Tells the event loop to exit with return code 0. This is the same as calling exit(0).
See also exit().
void QEventLoop::wakeUp()
Wakes the event loop if it is blocked waiting for events, so that it re-checks its state. It is safe to call from any thread.