20class Q_CORE_EXPORT
QTimer :
public QObject
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)
29 explicit QTimer(QObject *parent =
nullptr);
32 bool isActive()
const;
33 QBindable<
bool> bindableActive();
35 Qt::TimerId id()
const;
37 void setInterval(
int msec);
39 QBindable<
int> bindableInterval();
41 int remainingTime()
const;
43 void setTimerType(Qt::TimerType atype);
44 Qt::TimerType timerType()
const;
45 QBindable<Qt::TimerType> bindableTimerType();
47 void setSingleShot(
bool singleShot);
48 bool isSingleShot()
const;
49 QBindable<
bool> bindableSingleShot();
51 QT_CORE_INLINE_SINCE(6, 8)
52 static void singleShot(
int msec,
const QObject *receiver,
const char *member);
54 QT_CORE_INLINE_SINCE(6, 8)
55 static void singleShot(
int msec, Qt::TimerType timerType,
const QObject *receiver,
const char *member);
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);
65 template <
typename Duration,
typename Functor>
66 static inline void singleShot(Duration interval,
67 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
70 singleShot(interval, defaultTypeFor(interval), receiver, std::forward<Functor>(slot));
72 template <
typename Duration,
typename Functor>
73 static inline void singleShot(Duration interval, Qt::TimerType timerType,
74 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
77 using Prototype =
void(*)();
78 singleShotImpl(toDuration(interval), timerType, receiver,
79 QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(slot)));
84 template <
typename Duration,
typename Functor>
85 static inline void singleShot(Duration interval, Functor &&slot)
87 singleShot(interval, defaultTypeFor(interval),
nullptr, std::forward<Functor>(slot));
89 template <
typename Duration,
typename Functor>
90 static inline void singleShot(Duration interval, Qt::TimerType timerType, Functor &&slot)
92 singleShot(interval, timerType,
nullptr, std::forward<Functor>(slot));
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);
101 template <
typename ... Args>
102 QMetaObject::Connection callOnTimeout(Args && ...args)
104 return QObject::connect(
this, &QTimer::timeout, std::forward<Args>(args)... );
110 void start(
int msec);
116 void timeout(QPrivateSignal);
119 void setInterval(std::chrono::milliseconds value);
121 std::chrono::milliseconds intervalAsDuration()
const
123 return std::chrono::milliseconds(interval());
126 std::chrono::milliseconds remainingTimeAsDuration()
const
128 return std::chrono::milliseconds(remainingTime());
131#if QT_CORE_REMOVED_SINCE(6
, 8
)
132 static void singleShot(std::chrono::milliseconds value,
const QObject *receiver,
const char *member)
134 singleShot(value, defaultTypeFor(value), receiver, member);
136 static void singleShot(std::chrono::milliseconds interval, Qt::TimerType timerType,
137 const QObject *receiver,
const char *member);
139 static void singleShot(std::chrono::nanoseconds value,
const QObject *receiver,
const char *member)
141 singleShot(value, defaultTypeFor(value), receiver, member);
143 static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType,
144 const QObject *receiver,
const char *member);
146 void start(std::chrono::milliseconds value);
149 void timerEvent(QTimerEvent *) override;
152 Q_DISABLE_COPY(QTimer)
153 Q_DECLARE_PRIVATE(QTimer)
154 friend class QChronoTimer;
156 static std::chrono::nanoseconds from_msecs(std::chrono::milliseconds);
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
163 inline int startTimer(
int){
return -1;}
164 inline void killTimer(
int){}
166 static constexpr Qt::TimerType defaultTypeFor(
int msecs)
noexcept
167 {
return defaultTypeFor(std::chrono::milliseconds{msecs}); }
169#if QT_CORE_REMOVED_SINCE(6
, 8
)
170 static constexpr Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval)
noexcept
172 return defaultTypeFor(std::chrono::nanoseconds{interval});
176 static constexpr Qt::TimerType defaultTypeFor(std::chrono::nanoseconds interval)
noexcept
181 using namespace std::chrono_literals;
182 return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer;
185#if QT_CORE_REMOVED_SINCE(6
, 11
)
187 static void singleShotImpl(
int msec, Qt::TimerType timerType,
188 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
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);
194 static void singleShotImpl(std::chrono::nanoseconds interval, Qt::TimerType timerType,
195 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);