QAbstractEventDispatcher Class
The QAbstractEventDispatcher class provides an interface to manage Nt's event queue. More…
| Header | #include <QAbstractEventDispatcher> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QAbstractEventDispatcher(QObject *parent = nullptr) | |
| ~QAbstractEventDispatcher() | |
| bool | processEvents(QEventLoop::ProcessEventsFlags flags) |
| void | registerSocketNotifier(QSocketNotifier *notifier) |
| void | unregisterSocketNotifier(QSocketNotifier *notifier) |
| int | registerTimer(qint64 interval, Qt::TimerType timerType, QObject *object) |
| void | registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) |
| bool | unregisterTimer(int timerId) |
| bool | unregisterTimers(QObject *object) |
| QList<TimerInfo> | registeredTimers(QObject *object) const |
| int | remainingTime(int timerId) |
| void | wakeUp() |
| void | interrupt() |
| void | flush() |
Signals
| void | aboutToBlock() |
| void | awake() |
Static Public Members
| QAbstractEventDispatcher * | instance(QThread *thread = nullptr) |
| int | allocateTimerId() |
| void | releaseTimerId(int timerId) |
Detailed Description
Every thread has an event dispatcher that handles its posted events, timers and socket notifiers. QAbstractEventDispatcher gives access to it. Get the one of a thread with instance(), then register timers, wake it up when it waits, or connect to aboutToBlock() and awake():
QAbstractEventDispatcher *dispatcher = QAbstractEventDispatcher::instance();
QObject::connect(dispatcher, &QAbstractEventDispatcher::aboutToBlock, [] { flushCaches(); });
See also QEventLoop, QTimer, QSocketNotifier and QThread.
Differences from Qt
The class is not abstract and cannot be reimplemented: it is a handle onto the one dispatcher that Nt has for POSIX systems, which is not replaceable, and there is no QThread::setEventDispatcher(). A thread has one handle, which instance() creates on first use and which is deleted with the thread; an object that the constructor makes is another handle, and is the one that reports aboutToBlock() and awake() if it was the first for its thread. The functions work on the dispatcher of the thread that the handle lives in. Timer types are accepted and ignored, as are all timers of millisecond precision, and registeredTimers() reports Qt::CoarseTimer. registerSocketNotifier() and unregisterSocketNotifier() enable and disable a notifier, since a QSocketNotifier registers itself, and must be called from the thread of the dispatcher. There are no native event filters, and flush() does nothing.
Member Function Documentation
QAbstractEventDispatcher::QAbstractEventDispatcher(QObject *parent = nullptr)
Constructs a handle onto the event dispatcher of the calling thread, with the given parent. Use instance() instead, unless the handle has to have a parent.
See also instance().
QAbstractEventDispatcher::~QAbstractEventDispatcher()
Destroys the handle. The dispatcher itself belongs to the thread.
void QAbstractEventDispatcher::aboutToBlock()
This signal is emitted before the event dispatcher waits for events, from the thread that waits. Slots must be quick, since the thread is about to sleep and does not handle events before they return.
See also awake().
int QAbstractEventDispatcher::allocateTimerId()
Returns a timer identifier that is not in use. The identifiers are unique across all threads.
See also registerTimer() and releaseTimerId().
void QAbstractEventDispatcher::awake()
This signal is emitted after the event dispatcher woke up from waiting for events, from the thread that waited, and before it handles them.
See also aboutToBlock().
void QAbstractEventDispatcher::flush()
Does nothing. It exists because other dispatchers flush buffered output here.
QAbstractEventDispatcher * QAbstractEventDispatcher::instance(QThread *thread = nullptr)
Returns the event dispatcher of thread, or the one of the calling thread if thread is nullptr. It is created on first use, and valid as long as the thread lives. thread does not have to be running.
See also QThread::currentThread().
void QAbstractEventDispatcher::interrupt()
Makes the processEvents() call that waits for events, or the next one that would, return without waiting. It can be called from any thread.
See also wakeUp().
bool QAbstractEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags)
Processes pending events once, according to flags, and returns true if an event, a timer or a notifier was handled. With QEventLoop::WaitForMoreEvents it blocks until there is something to handle, or interrupt() is called. It has to be called from the thread that the dispatcher belongs to.
See also QEventLoop::processEvents() and interrupt().
QList<TimerInfo> QAbstractEventDispatcher::registeredTimers(QObject *object) const
Returns the timers that are registered for object.
See also registerTimer().
void QAbstractEventDispatcher::registerSocketNotifier(QSocketNotifier *notifier)
Enables notifier, which has to live in the thread of this dispatcher, so that it reports activity. A QSocketNotifier registers itself when it is created, so this is only needed after unregisterSocketNotifier().
See also unregisterSocketNotifier() and QSocketNotifier::setEnabled().
int QAbstractEventDispatcher::registerTimer(qint64 interval, Qt::TimerType timerType, QObject *object)
Registers a timer with the interval interval milliseconds for object, which has to live in the thread of this dispatcher, and returns its identifier. object gets a QTimerEvent with the identifier each time the timer fires. timerType is ignored.
See also unregisterTimer(), unregisterTimers() and registeredTimers().
void QAbstractEventDispatcher::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object)
This is an overloaded function.
Registers a timer with the identifier timerId, which comes from allocateTimerId(), the interval interval milliseconds and the type timerType, for object.
void QAbstractEventDispatcher::releaseTimerId(int timerId)
Gives back timerId, which was allocated with allocateTimerId(). Identifiers are never reused, so this does nothing.
See also allocateTimerId().
int QAbstractEventDispatcher::remainingTime(int timerId)
Returns the number of milliseconds until the timer timerId fires, 0 if it is overdue, or -1 if there is no such timer.
See also registerTimer().
void QAbstractEventDispatcher::unregisterSocketNotifier(QSocketNotifier *notifier)
Disables notifier, which has to live in the thread of this dispatcher, so that it stops reporting activity.
See also registerSocketNotifier() and QSocketNotifier::setEnabled().
bool QAbstractEventDispatcher::unregisterTimer(int timerId)
Unregisters the timer timerId and returns true if it was registered; otherwise returns false.
See also registerTimer().
bool QAbstractEventDispatcher::unregisterTimers(QObject *object)
Unregisters all timers of object and returns true if there were any; otherwise returns false.
See also unregisterTimer().
void QAbstractEventDispatcher::wakeUp()
Wakes the dispatcher if it is blocked waiting for events, so that it looks at them again. It can be called from any thread.
See also interrupt().