Qt Creator 实时日期+时间
1.创建一个QT Gui , 我的叫做mainwindow
2.在mainwindow.ui页面添加一个 lable 我这里还添加了背景图片
如下:
3.在mainwindow.h文件添加头文件
#include <QTimer>
#include <QDateTime>
public slots:
//实时显示日期 时间
void timerUpdate(void);
4.在mainwindow.cpp文件的构造函数内:
ui->setupUi(this);
QTimer * timer = new QTimer(this);//创建一个新的定时器
connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
timer->start(1000); //设置溢出时间为1s,并启动定时器
/*
* 显示系统时间
**/
void MainWindow::timerUpdate()
{
//创建 提供了日期和时间函数的类对象 QDateTime
QDateTime time = QDateTime::currentDateTime();
QTime time1 = QTime::currentTime();
//这里的 \t跳格 \n 换行
//QString str = time.toString("\tyyyy-MM-dd \n\thh:mm:ss dddd"); //年-月-日 时:分:秒 周几
QString text = time.toString("\tyyyy-MM-dd \n\thh:mm:ss"); //年-月-日 时:分:秒
//每隔一秒就将“:” 显示为空格
if(time1.second() % 2 == 0) text[19]=' ';
//将处理后的日期时间写入 lobal内 lobal的objectName为 timerUPdate
ui->timerUpdate->setText(text);
}
5.ctrl+r 编译运行看些效果:
基数秒 偶数秒 最后一个“:”会闪烁 ;
感谢观看 java scala hadoop c c++ qt 都可以一起交流 。。。