效果显示图
话不多说,直接上代码。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<qdebug.h>
#include"qtimer.h"
using namespace cv;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void timerUpdate(); //实时更新时间
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<iostream>
#include<math.h>
#include<QDateTimeEdit>
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//时间的实时显示(添加信号槽的连接)
QTimer *timer =new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerUpdate()
{
QDateTime time =QDateTime::currentDateTime();
QString str=time.toString("yyyy-MM-dd dddd hh:mm:ss");
ui->date1->setText(str); // ui->date1 是界面控件名称
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
qDebug()<<"www";
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}