定时器用于实现定时操作,如:
- 每三分钟保存一次
- 每500毫秒闪烁一次
- 每1秒刷新一次界面
新建Timer
(1) 重写timeEvent()函数
virtual void timerEvent ( QTimerEvent * event );
// 定时器处理函数
void Test8_2A::timerEvent ( QTimerEvent * event )
{
// 可以有多个定时器,每个的定时器有不同的
处理
if(event‐>timerId() == m_timerId)
{
}
}
(2) 启动定时器,指定时间间隔
m_timerId=startTime(500);
例子:实现一个文字时钟,显示系统当前时间

Timer_Event::Timer_Event(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
m_timerId = startTimer(1000);
}
void Timer_Event::timerEvent(QTimerEvent *event)
{
if (event->timerId()==m_timerId)
{
QTime now = QTime::currentTime();
QString text = now.toString("HH:mm:ss ap");
ui.labelTime->setText(text);
}
}
❤️

网友评论