您的位置:首页 > 编程语言 > Qt开发

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

2017-04-07 22:12 1386 查看
 
#include
<QLabel>
#include
<QDateTime>
#include
<QTimer>
#include
<QString>
 
QLabel
*currentTimeLabel
=
newQLabel;
QTimer
*timer
=
new
QTimer(this);

timer->start(1000);
// 每次发射timeout信号时间间隔为1秒
connect(timer,
SIGNAL(timeout()),
this,
SLOT(timeUpdate()));
 
void
MainWindow::timeUpdate()
{
   
QDateTime
CurrentTime
=
QDateTime::currentDateTime();
   
QString
Timestr
=
CurrentTime.toString(" 
yyyy年-MM月-dd日 
hh:mm:ss
 "); //
设置显示的格式
   
currentTimeLabel->setText(Timestr);
   
ui->statusbar->addWidget(currentTimeLabel);
}
(timeUpdate()槽函数要在头文件声明)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息