19class Q_CORE_EXPORT
QTimer :
public QObject
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)
28 explicit QTimer(QObject *parent =
nullptr);
31 bool isActive()
const;
32 QBindable<
bool> bindableActive();
34 Qt::TimerId id()
const;
36 void setInterval(
int msec);
38 QBindable<
int> bindableInterval();
40 int remainingTime()
const;
42 void setTimerType(Qt::TimerType atype);
43 Qt::TimerType timerType()
const;
44 QBindable<Qt::TimerType> bindableTimerType();
46 void setSingleShot(
bool singleShot);
47 bool isSingleShot()
const;
48 QBindable<
bool> bindableSingleShot();
50 QT_CORE_INLINE_SINCE(6, 8)
51 static void singleShot(
int msec,
const QObject *receiver,
const char *member);
53 QT_CORE_INLINE_SINCE(6, 8)
54 static void singleShot(
int msec, Qt::TimerType timerType,
const QObject *receiver,
const char *member);
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);
64 template <
typename Duration,
typename Functor>
65 static inline void singleShot(Duration interval,
66 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
69 singleShot(interval, defaultTypeFor(interval), receiver, std::forward<Functor>(slot));
71 template <
typename Duration,
typename Functor>
72 static inline void singleShot(Duration interval, Qt::TimerType timerType,
73 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
76 using Prototype =
void(*)();
77 singleShotImpl(toDuration(interval), timerType, receiver,
78 QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(slot)));
83 template <
typename Duration,
typename Functor>
84 static inline void singleShot(Duration interval, Functor &&slot)
86 singleShot(interval, defaultTypeFor(interval),
nullptr, std::forward<Functor>(slot));
88 template <
typename Duration,
typename Functor>
89 static inline void singleShot(Duration interval, Qt::TimerType timerType, Functor &&slot)
91 singleShot(interval, timerType,
nullptr, std::forward<Functor>(slot));
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);
100 template <
typename ... Args>
101 QMetaObject::Connection callOnTimeout(Args && ...args)
103 return QObject::connect(
this, &QTimer::timeout, std::forward<Args>(args)... );
109 void start(
int msec);
115 void timeout(QPrivateSignal);
118 void setInterval(std::chrono::milliseconds value);
120 std::chrono::milliseconds intervalAsDuration()
const
122 return std::chrono::milliseconds(interval());
125 std::chrono::milliseconds remainingTimeAsDuration()
const
127 return std::chrono::milliseconds(remainingTime());
130#if QT_CORE_REMOVED_SINCE(6
, 8
)
131 static void singleShot(std::chrono::milliseconds value,
const QObject *receiver,
const char *member)
133 singleShot(value, defaultTypeFor(value), receiver, member);
135 static void singleShot(std::chrono::milliseconds interval, Qt::TimerType timerType,
136 const QObject *receiver,
const char *member);
138 static void singleShot(std::chrono::nanoseconds value,
const QObject *receiver,
const char *member)
140 singleShot(value, defaultTypeFor(value), receiver, member);
142 static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType,
143 const QObject *receiver,
const char *member);
145 void start(std::chrono::milliseconds value);
148 void timerEvent(QTimerEvent *) override;
151 Q_DISABLE_COPY(QTimer)
152 Q_DECLARE_PRIVATE(QTimer)
153 friend class QChronoTimer;
155 static std::chrono::nanoseconds from_msecs(std::chrono::milliseconds);
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
162 inline int startTimer(
int){
return -1;}
163 inline void killTimer(
int){}
165 static constexpr Qt::TimerType defaultTypeFor(
int msecs)
noexcept
166 {
return defaultTypeFor(std::chrono::milliseconds{msecs}); }
168#if QT_CORE_REMOVED_SINCE(6
, 8
)
169 static constexpr Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval)
noexcept
171 return defaultTypeFor(std::chrono::nanoseconds{interval});
175 static constexpr Qt::TimerType defaultTypeFor(std::chrono::nanoseconds interval)
noexcept
180 using namespace std::chrono_literals;
181 return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer;
184#if QT_CORE_REMOVED_SINCE(6
, 11
)
186 static void singleShotImpl(
int msec, Qt::TimerType timerType,
187 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
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);
193 static void singleShotImpl(std::chrono::nanoseconds interval, Qt::TimerType timerType,
194 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);