Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtimer.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QTIMER_H
5#define QTIMER_H
6
7#include <QtCore/qglobal.h>
8
9#ifndef QT_NO_QOBJECT
10
11#include <QtCore/qbasictimer.h> // conceptual inheritance
12#include <QtCore/qobject.h>
13
14#include <chrono>
15
17
18class QTimerPrivate;
19class Q_CORE_EXPORT QTimer : public QObject
20{
21 Q_OBJECT
22 Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot BINDABLE bindableSingleShot)
23 Q_PROPERTY(int interval READ interval WRITE setInterval BINDABLE bindableInterval)
24 Q_PROPERTY(int remainingTime READ remainingTime)
25 Q_PROPERTY(Qt::TimerType timerType READ timerType WRITE setTimerType BINDABLE bindableTimerType)
26 Q_PROPERTY(bool active READ isActive STORED false BINDABLE bindableActive)
27public:
28 explicit QTimer(QObject *parent = nullptr);
29 ~QTimer();
30
31 bool isActive() const;
32 QBindable<bool> bindableActive();
33 int timerId() const;
34 Qt::TimerId id() const;
35
36 void setInterval(int msec);
37 int interval() const;
38 QBindable<int> bindableInterval();
39
40 int remainingTime() const;
41
42 void setTimerType(Qt::TimerType atype);
43 Qt::TimerType timerType() const;
44 QBindable<Qt::TimerType> bindableTimerType();
45
46 void setSingleShot(bool singleShot);
47 bool isSingleShot() const;
48 QBindable<bool> bindableSingleShot();
49
50 QT_CORE_INLINE_SINCE(6, 8)
51 static void singleShot(int msec, const QObject *receiver, const char *member);
52
53 QT_CORE_INLINE_SINCE(6, 8)
54 static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member);
55
56 // singleShot with context
57#ifdef Q_QDOC
58 template <typename Duration, typename Functor>
59 static inline void singleShot(Duration interval, const QObject *receiver, Functor &&slot);
60 template <typename Duration, typename Functor>
61 static inline void singleShot(Duration interval, Qt::TimerType timerType,
62 const QObject *receiver, Functor &&slot);
63#else
64 template <typename Duration, typename Functor>
65 static inline void singleShot(Duration interval,
66 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
67 Functor &&slot)
68 {
69 singleShot(interval, defaultTypeFor(interval), receiver, std::forward<Functor>(slot));
70 }
71 template <typename Duration, typename Functor>
72 static inline void singleShot(Duration interval, Qt::TimerType timerType,
73 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
74 Functor &&slot)
75 {
76 using Prototype = void(*)();
77 singleShotImpl(toDuration(interval), timerType, receiver,
78 QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(slot)));
79 }
80#endif
81
82 // singleShot without context
83 template <typename Duration, typename Functor>
84 static inline void singleShot(Duration interval, Functor &&slot)
85 {
86 singleShot(interval, defaultTypeFor(interval), nullptr, std::forward<Functor>(slot));
87 }
88 template <typename Duration, typename Functor>
89 static inline void singleShot(Duration interval, Qt::TimerType timerType, Functor &&slot)
90 {
91 singleShot(interval, timerType, nullptr, std::forward<Functor>(slot));
92 }
93
94#ifdef Q_QDOC
95 template <typename Functor>
96 QMetaObject::Connection callOnTimeout(Functor &&slot);
97 template <typename Functor>
98 QMetaObject::Connection callOnTimeout(const QObject *context, Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection);
99#else
100 template <typename ... Args>
101 QMetaObject::Connection callOnTimeout(Args && ...args)
102 {
103 return QObject::connect(this, &QTimer::timeout, std::forward<Args>(args)... );
104 }
105
106#endif
107
108public Q_SLOTS:
109 void start(int msec);
110
111 void start();
112 void stop();
113
114Q_SIGNALS:
115 void timeout(QPrivateSignal);
116
117public:
118 void setInterval(std::chrono::milliseconds value);
119
120 std::chrono::milliseconds intervalAsDuration() const
121 {
122 return std::chrono::milliseconds(interval());
123 }
124
125 std::chrono::milliseconds remainingTimeAsDuration() const
126 {
127 return std::chrono::milliseconds(remainingTime());
128 }
129
130#if QT_CORE_REMOVED_SINCE(6, 8)
131 static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
132 {
133 singleShot(value, defaultTypeFor(value), receiver, member);
134 }
135 static void singleShot(std::chrono::milliseconds interval, Qt::TimerType timerType,
136 const QObject *receiver, const char *member);
137#endif // QT_CORE_REMOVED_SINCE(6, 8)
138 static void singleShot(std::chrono::nanoseconds value, const QObject *receiver, const char *member)
139 {
140 singleShot(value, defaultTypeFor(value), receiver, member);
141 }
142 static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType,
143 const QObject *receiver, const char *member);
144
145 void start(std::chrono::milliseconds value);
146
147protected:
148 void timerEvent(QTimerEvent *) override;
149
150private:
151 Q_DISABLE_COPY(QTimer)
152 Q_DECLARE_PRIVATE(QTimer)
153 friend class QChronoTimer;
154
155 static std::chrono::nanoseconds from_msecs(std::chrono::milliseconds);
156
157 static std::chrono::nanoseconds toDuration(int msecs) noexcept
158 { return std::chrono::milliseconds(msecs); }
159 static std::chrono::nanoseconds toDuration(std::chrono::nanoseconds ns) noexcept
160 { return ns; }
161
162 inline int startTimer(int){ return -1;}
163 inline void killTimer(int){}
164
165 static constexpr Qt::TimerType defaultTypeFor(int msecs) noexcept
166 { return defaultTypeFor(std::chrono::milliseconds{msecs}); }
167
168#if QT_CORE_REMOVED_SINCE(6, 8)
169 static constexpr Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval) noexcept
170 {
171 return defaultTypeFor(std::chrono::nanoseconds{interval});
172 }
173#endif
174
175 static constexpr Qt::TimerType defaultTypeFor(std::chrono::nanoseconds interval) noexcept
176 {
177 // coarse timers are worst in their first firing
178 // so we prefer a high precision timer for something that happens only once
179 // unless the timeout is too big, in which case we go for coarse anyway
180 using namespace std::chrono_literals;
181 return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer;
182 }
183
184#if QT_CORE_REMOVED_SINCE(6, 11)
185 // was: QT_CORE_INLINE_SINCE(6, 8)
186 static void singleShotImpl(int msec, Qt::TimerType timerType,
187 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
188#endif
189#if QT_CORE_REMOVED_SINCE(6, 8)
190 static void singleShotImpl(std::chrono::milliseconds interval, Qt::TimerType timerType,
191 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
192#endif
193 static void singleShotImpl(std::chrono::nanoseconds interval, Qt::TimerType timerType,
194 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
195};
196
197#if QT_CORE_INLINE_IMPL_SINCE(6, 8)
198void QTimer::singleShot(int msec, const QObject *receiver, const char *member)
199{ singleShot(std::chrono::milliseconds{msec}, receiver, member); }
200
201void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver,
202 const char *member)
203{ singleShot(std::chrono::milliseconds{msec}, timerType, receiver, member); }
204#endif
205
206QT_END_NAMESPACE
207
208#endif // QT_NO_QOBJECT
209
210#endif // QTIMER_H
\inmodule QtCore
~QTimerPrivate() override
QTimerPrivate(QTimer *qq)
Definition qtimer_p.h:27
void setIntervalDuration(std::chrono::nanoseconds nsec)
Definition qtimer_p.h:42
static constexpr int INV_TIMER
Definition qtimer_p.h:40
void setInterval(int msec)
Definition qtimer_p.h:52
bool isActive() const
Definition qtimer_p.h:58
const bool isQTimer
Definition qtimer_p.h:70
QTimerPrivate(std::chrono::nanoseconds nsec, QChronoTimer *qq)
Definition qtimer_p.h:32
Qt::TimerId id
Definition qtimer_p.h:60
\inmodule QtCore
Definition qtimer.h:20
#define Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(...)
Definition qproperty.h:1335
#define Q_OBJECT_COMPUTED_PROPERTY(Class, Type, name, ...)
Definition qproperty.h:1424
#define Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(...)