From 72d660843b126aee356a637ba5b6c86c88f46f6b Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Mon, 20 Feb 2023 13:59:48 +0200 Subject: [PATCH] QCoreApplication: port processEvents() to QDeadlineTimer As Eddy pointed out in review, the existing overload API docs says using this method is discouraged; but adding a QDeadlineTimer overload is more about preventing overflow, as it can handle qint64 or chrono::milliseconds. So it's either add this new overload or change the existing one to take a qint64. [ChangeLog][QtCore][QCoreApplication] Added processEvents() overload that takes a QDeadlineTimer. Task-number: QTBUG-110059 Change-Id: I02f938ee8243c09e493bd88ed496b862d87910f7 Reviewed-by: Thiago Macieira Reviewed-by: Marc Mutz --- src/corelib/kernel/qcoreapplication.cpp | 27 +++++++++++++++++++------ src/corelib/kernel/qcoreapplication.h | 2 ++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index d532875c20..c119074fc7 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -35,7 +35,6 @@ #include #endif #endif -#include #include #include #include @@ -1349,12 +1348,29 @@ void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags) } /*! - \overload processEvents() + \overload Processes pending events for the calling thread for \a ms milliseconds or until there are no more events to process, whichever is shorter. + This is equivalent to calling: + \code + QCoreApplication::processEvents(flags, QDeadlineTimer(ms)); + \endcode +*/ +void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, int ms) +{ + QCoreApplication::processEvents(flags, QDeadlineTimer(ms)); +} + +/*! + \since 6.7 + \overload + + Processes pending events for the calling thread untile \a deadline has expired, + or until there are no more events to process, whichever happens first. + Use of this function is discouraged. Instead, prefer to move long operations out of the GUI thread into an auxiliary one and to completely avoid nested event loop processing. If event processing is really @@ -1372,7 +1388,7 @@ void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags) \sa exec(), QTimer, QEventLoop::processEvents() */ -void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, int ms) +void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline) { // ### TODO: consider splitting this method into a public and a private // one, so that a user-invoked processEvents can be detected @@ -1380,10 +1396,9 @@ void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, int m QThreadData *data = QThreadData::current(); if (!data->hasEventDispatcher()) return; - QElapsedTimer start; - start.start(); + while (data->eventDispatcher.loadRelaxed()->processEvents(flags & ~QEventLoop::WaitForMoreEvents)) { - if (start.elapsed() > ms) + if (deadline.hasExpired()) break; } } diff --git a/src/corelib/kernel/qcoreapplication.h b/src/corelib/kernel/qcoreapplication.h index 37ed9890ed..aa9fe58e2e 100644 --- a/src/corelib/kernel/qcoreapplication.h +++ b/src/corelib/kernel/qcoreapplication.h @@ -8,6 +8,7 @@ #include #ifndef QT_NO_QOBJECT #include +#include #include #include #else @@ -93,6 +94,7 @@ public: static int exec(); static void processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents); static void processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime); + static void processEvents(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline); static bool sendEvent(QObject *receiver, QEvent *event); static void postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority);