QTimer的用法比较简单,看官方提供的使用例:
QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(1000);
每秒超时调用this->update()。
QT也提供了另外一个类:QBasicTimer,其用法例:
#include <QCoreApplication>
#include <QBasicTimer>
#include <QtCore/QDebug>
class MyClass:public QObject
{
public:
MyClass(){};
protected:
void timerEvent( QTimerEvent *event ){
qDebug() << "Time out timer id" << event->timerId();
}
};
int main( int argc, char *argv[] )
{
QCoreApplication app( argc, argv );
MyClass *mc = new MyClass();
QBasicTimer bt;
bt.start( 120 * 1000, mc );
return app.exec();
}
发生超时事件的时候,会调用的mc->timerEvent(),而没有timerout()信号。
在WebKit的分析中有碰到QBasicTimer的使用。