廊坊疫情最新情况沈阳seo排名优化推广
目录
实现效果
源码实现
头文件
核心代码
槽函数
实现效果
一秒一秒实时刷新显示时间。
源码实现
头文件
#include <QLabel>
#include <QTimer>
#include <QDateTime>
#include <QString>
核心代码
this->resize(500,500);
this->setWindowTitle("testWindow");//定义一个label显示时间
QLabel *timeLabel=new QLabel(this);
timeLabel->setObjectName("timeLab");//起名字,方便后续查找
timeLabel->setStyleSheet("QLabel {color:rgb(0,255,0)}""QLabel {font:16pt; font-family:'Microsoft YaHei'}");//获取当前时间
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");//时间格式转换
timeLabel->setText(str);//label上显示
timeLabel->setAlignment(Qt::AlignHCenter);//居中对齐//定义一个定时器,一秒刷新一次时间
QTimer *time1=new QTimer;
connect(time1,SIGNAL(timeout()),this,SLOT(timeLabelUpdate()));//连接信号槽
time1->start(1000);
槽函数
void timeLabelUpdate()函数实现
QLabel *label=this->findChild<QLabel *>("timeLab");//找名字为timeLab的儿子QDateTime time = QDateTime::currentDateTime();QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");label->setText(str);