要在标签上实时显示系统时间,首先要获取系统时间,然后运用定时器每一秒更新一次时间;
效果:
具体步骤如下:
1.创建QDateTime对象获取当前日期和时间
dateTime =QDateTime::currentDateTime();//获取系统时间
2.创建QLabel显示时间
showCurrentTime = new QLabel(dateTime.toString("yyyy-MM-dd hh:mm:ss"),this);
3.创建定时器定时更新时间和日期
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timeUpdate()));//连接信号槽
timer->start(1000);//1s更新一次
槽函数中获取当前时间,并进行更新;时间每1s更新一次
void Widget::timeUpdate()
{
dateTime =QDateTime::currentDateTime();//获取当前系统时间
showCurrentTime->setText(dateTime.toString("yyyy-MM-dd hh:mm:ss"));
}
本文介绍了如何在Qt环境中通过QDateTime和QTimer实现QLabel组件实时显示系统时间的功能。首先创建QDateTime对象获取当前时间,然后利用QLabel显示,接着设置定时器每秒触发一次timeUpdate()槽函数,更新时间显示。
854

被折叠的 条评论
为什么被折叠?



