From 28161ab42e4a79d82118240078072e72c391af10 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Wed, 28 Aug 2024 22:17:14 +0300 Subject: [PATCH] QTimer: QChronoTimer isn't necessarily going to supersede QTimer ... since no consensus has been reached about what to do in Qt7 (whether to fold QChronoTimer back into QTimer or let QTimer be as-is to not break any existing code and use QChronoTimer in new code). Task-number: QTBUG-128426 Change-Id: I7d09b803b4f0df3ea7f48a5d900d0ae4be721d7b Reviewed-by: Marc Mutz Reviewed-by: Thiago Macieira (cherry picked from commit 87030778969165a285af2d919d6d7293e8078b6a) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/doc/src/timers.qdoc | 13 +++++++------ src/corelib/kernel/qchronotimer.cpp | 5 +++++ src/corelib/kernel/qtimer.cpp | 17 +++++++++-------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/corelib/doc/src/timers.qdoc b/src/corelib/doc/src/timers.qdoc index dc205a47b99..dbdd9754aa9 100644 --- a/src/corelib/doc/src/timers.qdoc +++ b/src/corelib/doc/src/timers.qdoc @@ -31,16 +31,17 @@ stop all timers in the object's thread; it is not possible to start timers for objects in another thread. - The main API for the timer functionality was \l QTimer. QTimer stores + The main API for the timer functionality is \l QTimer. QTimer stores the interval in a signed integer, which limits the maximum interval it supports to the number of milliseconds that can fit in a signed integer (in practice, this is a period of around 24 days). - Qt 6.8 introduced the \l QChronoTimer class to replace QTimer. QChronoTimer - stores the interval as \c std::chrono::nanoseconds, which means that - the maximum interval it supports is around 292 years. This mitigates - the chances of integer overflow that QTimer had if the interval was more - than \c{std::numeric_limits::max()}. + Qt 6.8 introduced the \l QChronoTimer class. The main difference between + the two classes is that QChronoTimer supports a larger interval range + and a higher precision (\c std::chrono::nanoseconds). For QTimer the + maximum supported interval is ±24 days, whereas for QChronoTimer it is + ±292 years. If you only need millisecond resolution and ±24 days range, + you can continue to use \l QTimer. The accuracy of the timers depends on the underlying operating system. Windows 2000 has 15ms accuracy; other systems that we have tested can diff --git a/src/corelib/kernel/qchronotimer.cpp b/src/corelib/kernel/qchronotimer.cpp index 61b3ec7ca17..50f8135a688 100644 --- a/src/corelib/kernel/qchronotimer.cpp +++ b/src/corelib/kernel/qchronotimer.cpp @@ -92,6 +92,11 @@ QT_BEGIN_NAMESPACE \section1 Alternatives to QChronoTimer + QChronoTimer provides nanosecond resolution and a ±292 years range + (less chances of integer overflow if the interval is longer than \c + std::numeric_limits::max()). If you only need millisecond resolution + and ±24 days range, you can continue to use the classical QTimer class + An alternative to using QChronoTimer is to call QObject::startTimer() for your object and reimplement the QObject::timerEvent() event handler in your class (which must be a sub-class of QObject). The disadvantage diff --git a/src/corelib/kernel/qtimer.cpp b/src/corelib/kernel/qtimer.cpp index 683b6dd1f9d..40bde5c06cc 100644 --- a/src/corelib/kernel/qtimer.cpp +++ b/src/corelib/kernel/qtimer.cpp @@ -74,13 +74,6 @@ QT_BEGIN_NAMESPACE more and more platforms, we expect that zero-millisecond QTimer objects will gradually be replaced by \l{QThread}s. - \note Since Qt 6.8 this class is superseded by \l{QChronoTimer}. - The maximum interval QTimer supports is limited by the number of - milliseconds that would fit in an \c int (which is around 24 days); - whereas QChronoTimer stores its interval as \c std::chrono::nanoseconds - (which raises that limit to ±292 years), that is, there is - less chance of integer overflow with QChronoTimer. - \section1 Accuracy and Timer Resolution The accuracy of timers depends on the underlying operating system @@ -103,7 +96,15 @@ QT_BEGIN_NAMESPACE \section1 Alternatives to QTimer - An alternative to using QTimer is to call QObject::startTimer() + Qt 6.8 introduced QChronoTimer. The main difference between the two + classes, is that QChronoTimer supports a larger interval range and a + higher precision (\c std::chrono::nanoseconds). For QTimer the maximum + supported interval is ±24 days, whereas for QChronoTimer it is ±292 + years (less chances of interger overflow with intervals longer than + \c std::numeric_limits::max()). If you only need millisecond + resolution and ±24 days range, you can continue to use QTimer. + + Another alternative is to call QObject::startTimer() for your object and reimplement the QObject::timerEvent() event handler in your class (which must inherit QObject). The disadvantage is that timerEvent() does not support such