Qt入门 Qt中时间设置(五)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010468553/article/details/79651624

Qtimer

QTimer类提供了重复和单次触发信号的定时器。

重复触发 - 时钟

QTimer类为定时器提供了一个高级别的编程接口。很容易使用:首先,创建一个QTimer,连接timeout()信号到适当的槽函数,并调用start(),然后在恒定的时间间隔会发射timeout()信号。

利用Qtimer,可以很轻松的模拟出一个时钟:

QTimer* timer = new QTimer(this);
//若当前时间超时,过了1000ms,更新m_pzEffectWidget窗口
connect(timer, SIGNAL(timeout()), m_pzEffectWidget, SLOT(update()));
timer->start(1000);

单次触发

使用setSingleShot(true)

QTimer *timer = new QTimer(this);
timer->setSingleShot(true);//设置为true之后,就会只触发一次
//过1000ms即触发一次
connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing()));
timer->start(1000);

使用静态函数QTimer::singleShot()

//过200ms触发
QTimer::singleShot(200, this, SLOT(updateCaption()));

猜你喜欢

转载自blog.csdn.net/u010468553/article/details/79651624