QT:Label控件动态显示当前时间
(2014-12-29 14:49:11)大家都知道在Label 标签上显示的时间都是静态的
那么怎样才能够让时间动态显示呢?这里我告诉大家一个使用定时器来刷新时间的方法
1.首先我们需要在chao槽函数中声明一个更新时间的函数:timerUpdate()
2.然后在构造函数中关联定时器计满信号和相应的槽函数。
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
timer->start(1000);
3.最后写实现函数:
void MainWindow::timerUpdate()
{
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->label->setText(str);
}