【Qt】QLabel实时显示系统时间

要在标签上实时显示系统时间,首先要获取系统时间,然后运用定时器每一秒更新一次时间;

效果:

具体步骤如下:

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"));
}

猜你喜欢

转载自blog.csdn.net/logani/article/details/127795150