6.0.0-rc4Source

QEventLoop Class

The QEventLoop class provides a means of entering and leaving an event loop. More…

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

List of all members, including inherited members

Public Types

Public Functions

QEventLoop(QObject *parent = nullptr)
~QEventLoop()
boolprocessEvents(ProcessEventsFlags flags = AllEvents)
voidprocessEvents(ProcessEventsFlags flags, int maxTime)
voidprocessEvents(ProcessEventsFlags flags, QDeadlineTimer deadline)
intexec(ProcessEventsFlags flags = AllEvents)
voidexit(int returnCode = 0)
boolisRunning() const
voidwakeUp()

Public Slots

voidquit()

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

ConstantDescription
QEventLoop::AllEventsAll pending events are processed.
QEventLoop::ExcludeUserInputEventsAccepted for compatibility; it has no effect because Nt has no user input events.
QEventLoop::ExcludeSocketNotifiersAccepted for compatibility; it has no effect, socket notifiers are always processed.
QEventLoop::WaitForMoreEventsBlock 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

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

Constructs an event loop with the given parent.

[override] 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.

See also quit() and exec().

bool QEventLoop::isRunning() const

Returns true if the event loop is running in exec(); otherwise returns false.

See also exec() and exit().

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.

[slot] 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.