Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-12 09:23:40

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:significant reason:default
0004 
0005 #ifndef QTIMER_H
0006 #define QTIMER_H
0007 
0008 #include <QtCore/qglobal.h>
0009 
0010 #ifndef QT_NO_QOBJECT
0011 
0012 #include <QtCore/qbasictimer.h> // conceptual inheritance
0013 #include <QtCore/qobject.h>
0014 
0015 #include <chrono>
0016 
0017 QT_BEGIN_NAMESPACE
0018 
0019 class QTimerPrivate;
0020 class Q_CORE_EXPORT QTimer : public QObject
0021 {
0022     Q_OBJECT
0023     Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot BINDABLE bindableSingleShot)
0024     Q_PROPERTY(int interval READ interval WRITE setInterval BINDABLE bindableInterval)
0025     Q_PROPERTY(int remainingTime READ remainingTime)
0026     Q_PROPERTY(Qt::TimerType timerType READ timerType WRITE setTimerType BINDABLE bindableTimerType)
0027     Q_PROPERTY(bool active READ isActive STORED false BINDABLE bindableActive)
0028 public:
0029     explicit QTimer(QObject *parent = nullptr);
0030     ~QTimer();
0031 
0032     bool isActive() const;
0033     QBindable<bool> bindableActive();
0034     int timerId() const;
0035     Qt::TimerId id() const;
0036 
0037     void setInterval(int msec);
0038     int interval() const;
0039     QBindable<int> bindableInterval();
0040 
0041     int remainingTime() const;
0042 
0043     void setTimerType(Qt::TimerType atype);
0044     Qt::TimerType timerType() const;
0045     QBindable<Qt::TimerType> bindableTimerType();
0046 
0047     void setSingleShot(bool singleShot);
0048     bool isSingleShot() const;
0049     QBindable<bool> bindableSingleShot();
0050 
0051     QT_CORE_INLINE_SINCE(6, 8)
0052     static void singleShot(int msec, const QObject *receiver, const char *member);
0053 
0054     QT_CORE_INLINE_SINCE(6, 8)
0055     static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member);
0056 
0057     // singleShot with context
0058 #ifdef Q_QDOC
0059     template <typename Duration, typename Functor>
0060     static inline void singleShot(Duration interval, const QObject *receiver, Functor &&slot);
0061     template <typename Duration, typename Functor>
0062     static inline void singleShot(Duration interval, Qt::TimerType timerType,
0063                                   const QObject *receiver, Functor &&slot);
0064 #else
0065     template <typename Duration, typename Functor>
0066     static inline void singleShot(Duration interval,
0067                                   const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
0068                                   Functor &&slot)
0069     {
0070         singleShot(interval, defaultTypeFor(interval), receiver, std::forward<Functor>(slot));
0071     }
0072     template <typename Duration, typename Functor>
0073     static inline void singleShot(Duration interval, Qt::TimerType timerType,
0074                                   const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
0075                                   Functor &&slot)
0076     {
0077         using Prototype = void(*)();
0078         singleShotImpl(toDuration(interval), timerType, receiver,
0079                        QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(slot)));
0080     }
0081 #endif
0082 
0083     // singleShot without context
0084     template <typename Duration, typename Functor>
0085     static inline void singleShot(Duration interval, Functor &&slot)
0086     {
0087         singleShot(interval, defaultTypeFor(interval), nullptr, std::forward<Functor>(slot));
0088     }
0089     template <typename Duration, typename Functor>
0090     static inline void singleShot(Duration interval, Qt::TimerType timerType, Functor &&slot)
0091     {
0092         singleShot(interval, timerType, nullptr, std::forward<Functor>(slot));
0093     }
0094 
0095 #ifdef Q_QDOC
0096     template <typename Functor>
0097     QMetaObject::Connection callOnTimeout(Functor &&slot);
0098     template <typename Functor>
0099     QMetaObject::Connection callOnTimeout(const QObject *context, Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection);
0100 #else
0101     template <typename ... Args>
0102     QMetaObject::Connection callOnTimeout(Args && ...args)
0103     {
0104         return QObject::connect(this, &QTimer::timeout, std::forward<Args>(args)... );
0105     }
0106 
0107 #endif
0108 
0109 public Q_SLOTS:
0110     void start(int msec);
0111 
0112     void start();
0113     void stop();
0114 
0115 Q_SIGNALS:
0116     void timeout(QPrivateSignal);
0117 
0118 public:
0119     void setInterval(std::chrono::milliseconds value);
0120 
0121     std::chrono::milliseconds intervalAsDuration() const
0122     {
0123         return std::chrono::milliseconds(interval());
0124     }
0125 
0126     std::chrono::milliseconds remainingTimeAsDuration() const
0127     {
0128         return std::chrono::milliseconds(remainingTime());
0129     }
0130 
0131 #if QT_CORE_REMOVED_SINCE(6, 8)
0132     static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
0133     {
0134         singleShot(value, defaultTypeFor(value), receiver, member);
0135     }
0136     static void singleShot(std::chrono::milliseconds interval, Qt::TimerType timerType,
0137                            const QObject *receiver, const char *member);
0138 #endif // QT_CORE_REMOVED_SINCE(6, 8)
0139     static void singleShot(std::chrono::nanoseconds value, const QObject *receiver, const char *member)
0140     {
0141         singleShot(value, defaultTypeFor(value), receiver, member);
0142     }
0143     static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType,
0144                            const QObject *receiver, const char *member);
0145 
0146     void start(std::chrono::milliseconds value);
0147 
0148 protected:
0149     void timerEvent(QTimerEvent *) override;
0150 
0151 private:
0152     Q_DISABLE_COPY(QTimer)
0153     Q_DECLARE_PRIVATE(QTimer)
0154     friend class QChronoTimer;
0155 
0156     static std::chrono::nanoseconds from_msecs(std::chrono::milliseconds);
0157 
0158     static std::chrono::nanoseconds toDuration(int msecs) noexcept
0159     { return std::chrono::milliseconds(msecs); }
0160     static std::chrono::nanoseconds toDuration(std::chrono::nanoseconds ns) noexcept
0161     { return ns; }
0162 
0163     inline int startTimer(int){ return -1;}
0164     inline void killTimer(int){}
0165 
0166     static constexpr Qt::TimerType defaultTypeFor(int msecs) noexcept
0167     { return defaultTypeFor(std::chrono::milliseconds{msecs}); }
0168 
0169 #if QT_CORE_REMOVED_SINCE(6, 8)
0170     static constexpr Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval) noexcept
0171     {
0172         return defaultTypeFor(std::chrono::nanoseconds{interval});
0173     }
0174 #endif
0175 
0176     static constexpr Qt::TimerType defaultTypeFor(std::chrono::nanoseconds interval) noexcept
0177     {
0178         // coarse timers are worst in their first firing
0179         // so we prefer a high precision timer for something that happens only once
0180         // unless the timeout is too big, in which case we go for coarse anyway
0181         using namespace std::chrono_literals;
0182         return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer;
0183     }
0184 
0185 #if QT_CORE_REMOVED_SINCE(6, 11)
0186     // was: QT_CORE_INLINE_SINCE(6, 8)
0187     static void singleShotImpl(int msec, Qt::TimerType timerType,
0188                                const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
0189 #endif
0190 #if QT_CORE_REMOVED_SINCE(6, 8)
0191     static void singleShotImpl(std::chrono::milliseconds interval, Qt::TimerType timerType,
0192                                const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
0193 #endif
0194     static void singleShotImpl(std::chrono::nanoseconds interval, Qt::TimerType timerType,
0195                                const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
0196 };
0197 
0198 #if QT_CORE_INLINE_IMPL_SINCE(6, 8)
0199 void QTimer::singleShot(int msec, const QObject *receiver, const char *member)
0200 { singleShot(std::chrono::milliseconds{msec}, receiver, member); }
0201 
0202 void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver,
0203                         const char *member)
0204 { singleShot(std::chrono::milliseconds{msec}, timerType, receiver, member); }
0205 #endif
0206 
0207 QT_END_NAMESPACE
0208 
0209 #endif // QT_NO_QOBJECT
0210 
0211 #endif // QTIMER_H