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的使用。

本文介绍了Qt中两种定时器QTimer与QBasicTimer的使用方法。QTimer通过信号槽机制实现定时任务,而QBasicTimer则通过重写timerEvent来响应定时事件。文章通过示例展示了两者的具体应用。

被折叠的 条评论
为什么被折叠?



