diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp index f852988e9a2..c12ed147db7 100644 --- a/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp @@ -156,6 +156,17 @@ MyObject::MyObject(QObject *parent) startTimer(50); // 50-millisecond timer startTimer(1000); // 1-second timer startTimer(60000); // 1-minute timer + + using namespace std::chrono; + startTimer(milliseconds(50)); + startTimer(seconds(1)); + startTimer(minutes(1)); + + // since C++14 we can use std::chrono::duration literals, e.g.: + startTimer(100ms); + startTimer(5s); + startTimer(2min); + startTimer(1h); } void MyObject::timerEvent(QTimerEvent *event) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index fd1c767b4c7..ac1b2d41bf7 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -1633,6 +1633,45 @@ int QObject::startTimer(int interval, Qt::TimerType timerType) return timerId; } +/*! + \since 5.9 + \overload + \fn int QObject::startTimer(std::chrono::milliseconds time, Qt::TimerType timerType) + + Starts a timer and returns a timer identifier, or returns zero if + it could not start a timer. + + A timer event will occur every \a time interval until killTimer() + is called. If \a interval is equal to \c{std::chrono::duration::zero()}, + then the timer event occurs once every time there are no more window + system events to process. + + The virtual timerEvent() function is called with the QTimerEvent + event parameter class when a timer event occurs. Reimplement this + function to get timer events. + + If multiple timers are running, the QTimerEvent::timerId() can be + used to find out which timer was activated. + + Example: + + \snippet code/src_corelib_kernel_qobject.cpp 8 + + Note that QTimer's accuracy depends on the underlying operating system and + hardware. The \a timerType argument allows you to customize the accuracy of + the timer. See Qt::TimerType for information on the different timer types. + Most platforms support an accuracy of 20 milliseconds; some provide more. + If Qt is unable to deliver the requested number of timer events, it will + silently discard some. + + The QTimer class provides a high-level programming interface with + single-shot timers and timer signals instead of events. There is + also a QBasicTimer class that is more lightweight than QTimer and + less clumsy than using timer IDs directly. + + \sa timerEvent(), killTimer(), QTimer::singleShot() +*/ + /*! Kills the timer with timer identifier, \a id. diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 25acdefeaf0..d42eb2a13db 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -55,6 +55,10 @@ #include +#if QT_HAS_INCLUDE() +# include +#endif + QT_BEGIN_NAMESPACE @@ -150,6 +154,13 @@ public: void moveToThread(QThread *thread); int startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer); +#if QT_HAS_INCLUDE() || defined(Q_QDOC) + Q_ALWAYS_INLINE + int startTimer(std::chrono::milliseconds time, Qt::TimerType timerType = Qt::CoarseTimer) + { + return startTimer(int(time.count()), timerType); + } +#endif void killTimer(int id); template