当前位置: 代码迷 >> 综合 >> Qt Creator 界面设计 显示当前时间
  详细解决方案

Qt Creator 界面设计 显示当前时间

热度:88   发布时间:2023-12-16 08:02:21.0

效果显示图

话不多说,直接上代码。

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_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic: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();
}





 

  相关解决方案