mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QTimer>
#include <QDateTime>
#include <QMainWindow>
#include <QLabel>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void TimeUpdate(QLabel label);
private:
QLabel *label;
QLabel *label1;
};
#endif // MAINWINDOW_H
mainwindow.c文件
#include "mainwindow.h"
#include <QTimer>
#include <QDateTime>
#include <QMainWindow>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
label = new QLabel(this);
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(TimeUpdate(label)));//将定时器与TimeUpdate函数绑定
timer->start(100);//一秒计时一次
}
MainWindow::~MainWindow()
{
}
void MainWindow::TimeUpdate(QLabel label) //显示系统时间的功能
{
//label = new QLabel(this);
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("日期: yyyy年MM月dd日 hh:mm:ss ddd");
label.setText(str);
}
版本:6.2.1
报错信息