Qt 获取系统时间并动态显示在窗口状态栏

 

#include

#include

#include

#include

 

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()槽函数要在头文件声明)

你可能感兴趣的:(qt5,QDateTime,QTimer,ui-statusbar)