首页 > 代码库 > QT源码阅读 -- QTimer

QT源码阅读 -- QTimer

#ifndef QTIMER_H
#define QTIMER_H

#ifndef QT_NO_QOBJECT

#include <QtCore/qbasictimer.h> // conceptual inheritance
#include <QtCore/qobject.h>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

QT_MODULE(Core)<span style="white-space:pre">//处理QT许可证信息;

class Q_CORE_EXPORT QTimer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot)
    Q_PROPERTY(int interval READ interval WRITE setInterval)
    Q_PROPERTY(bool active READ isActive)
public:
    explicit QTimer(QObject *parent = 0);<span style="white-space:pre">	//防止隐式转换;
#ifdef QT3_SUPPORT
    QT3_SUPPORT_CONSTRUCTOR QTimer(QObject *parent, const char *name);
#endif
    ~QTimer();

    inline bool isActive() const { return id >= 0; }<span style="white-space:pre">//是否处于激活状态,通过判断id是否大于0判断;
    int timerId() const { return id; }// 返回timer ID;

    void setInterval(int msec);// 设置间隔时间,既超时时间;
    int interval() const { return inter; }//获取间隔时间;

    inline void setSingleShot(bool singleShot);<span style="white-space:pre">// 设置是否属于SingleShot,既是否只触发一次;
    inline bool isSingleShot() const { return single; }// 获取是否是SingleShot;

    static void singleShot(int msec, QObject *receiver, const char *member);//这个静态函数非常方便使用,我们既可以不创建一个QTimer对象,也可不用发射一个信号出去,就可以完成timer的功能, QTimer::singleShot(600000, &app, SLOT(quit()));

public Q_SLOTS:<span style="white-space:pre">// QT4.1引入的,其实就是用来替代slots;
    void start(int msec);<span style="white-space:pre">// start slot;

    void start();
    void stop();// stop slot;

#ifdef QT3_SUPPORT
    inline QT_MOC_COMPAT void changeInterval(int msec) { start(msec); }
    QT_MOC_COMPAT int start(int msec, bool sshot);
#endif

Q_SIGNALS:
    void timeout();// timeout 信号;

protected:
    void timerEvent(QTimerEvent *);<span style="white-space:pre">// 对 QObject::timerEvent的reimplemented;

private:
    Q_DISABLE_COPY(QTimer)<span style="white-space:pre">

    inline int startTimer(int){ return -1;}
    inline void killTimer(int){}

    int id, inter, del;
    uint single : 1;
    uint nulltimer : 1;
};

inline void QTimer::setSingleShot(bool asingleShot) { single = asingleShot; }

QT_END_NAMESPACE

QT_END_HEADER

#endif // QT_NO_QOBJECT

#endif // QTIMER_H

QT源码阅读 -- QTimer