Qt学习之路—–QTimer

August 5th, 2013 by JasonLe's Tech Leave a reply »

QTimer是个定时类,可以通过SIGNAL 与 SLOT 触发某个时间,需要继承QTimer类。然后在构造函数中创建该对象,然后设置触发时间间隔。
在connect()函数中设置SLOT执行函数。

MyTimer::MyTimer(QObject *parent) :
QObject(parent)
{
time = new QTimer(this);
connect(time,SIGNAL(timeout()),this,SLOT(MySlot()));

time->start(2000);
}

MyTimer.h

#include <QObject>
#include<QTimer>

class MyTimer : public QObject
{
Q_OBJECT
public:
explicit MyTimer(QObject *parent = 0);
QTimer *time;

signals:

public slots:
void MySlot();
};