#include<QLabel>
#include<QDateTime>
#include<QTimer>
#include<QString>
QLabel *currentTimeLabel=new QLabel;
QTimer *timer=new QTimer(this);
timer->start(1000); // 每次发射timeout信号时间间隔为1秒
connect(timer,SIGNAL(timeout()),this,SLOT(timeUpdate()));
voidMainWindow::timeUpdate()
{
QDateTime CurrentTime=QDateTime::currentDateTime();
QString Timestr=CurrentTime.toString(" yyyy年-MM月-dd日 hh:mm:ss "); //设置显示的格式
currentTimeLabel->setText(Timestr);
ui->statusbar->addWidget(currentTimeLabel);
}
(timeUpdate()槽函数要在头文件声明)