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