1、效果图(显示了年-月-日 时:分:秒:毫秒)
2、头文件QtGuiApplicationTest.h
#pragma once
//头文件的添加(QDateTime、QTimer)
#include <QtWidgets/QMainWindow>
#include <QDateTime>
#include <QTimer>
#include "ui_QtGuiApplicationTest.h"
class QtGuiApplicationTest : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplicationTest(QWidget *parent = Q_NULLPTR);
private:
Ui::QtGuiApplication ui;
protected:
//重写函数timerEvent
virtual void timerEvent(QTimerEvent*);
};
3、实现QtGuiApplicationTest.cpp
#include "QtGuiApplicationTest.h"
QtGuiApplicationTest::QtGuiApplicationTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//启动定时器
startTimer(20);
}
void QtGuiApplicationTest::timerEvent(QTimerEvent* qte)
{
QString dt1 = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz");
ui.label_Time->setText(dt1);
}