From 0a0afb1a0d231fec7a54f22d6e1e340719faba0d Mon Sep 17 00:00:00 2001 From: flykhan Date: Thu, 10 Aug 2023 12:05:02 +0800 Subject: [PATCH] =?UTF-8?q?day4:=20=E5=9F=BA=E4=BA=8Eday3=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0Timer=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project02/.gitignore | 73 ++++++++++++++++++++++++ Project02/Project02.pro | 31 +++++++++++ Project02/main.cpp | 11 ++++ Project02/mainwindow.cpp | 15 +++++ Project02/mainwindow.h | 21 +++++++ Project02/mainwindow.ui | 22 ++++++++ qtdemo03/delayform2.cpp | 36 ++++++++++++ qtdemo03/delayform2.h | 26 +++++++++ qtdemo03/delayform2.ui | 117 +++++++++++++++++++++++++++++++++++++++ qtdemo03/form7.cpp | 15 +++-- qtdemo03/form7.h | 2 + qtdemo03/form7.ui | 30 +++++++++- qtdemo03/form8.cpp | 8 ++- qtdemo03/form8.h | 4 +- qtdemo03/main.cpp | 6 +- qtdemo03/qtdemo03.pro | 13 ++++- qtdemo03/timerform1.cpp | 54 ++++++++++++++++++ qtdemo03/timerform1.h | 34 ++++++++++++ qtdemo03/timerform1.ui | 81 +++++++++++++++++++++++++++ 19 files changed, 584 insertions(+), 15 deletions(-) create mode 100644 Project02/.gitignore create mode 100644 Project02/Project02.pro create mode 100644 Project02/main.cpp create mode 100644 Project02/mainwindow.cpp create mode 100644 Project02/mainwindow.h create mode 100644 Project02/mainwindow.ui create mode 100644 qtdemo03/delayform2.cpp create mode 100644 qtdemo03/delayform2.h create mode 100644 qtdemo03/delayform2.ui create mode 100644 qtdemo03/timerform1.cpp create mode 100644 qtdemo03/timerform1.h create mode 100644 qtdemo03/timerform1.ui diff --git a/Project02/.gitignore b/Project02/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/Project02/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/Project02/Project02.pro b/Project02/Project02.pro new file mode 100644 index 0000000..0086fd6 --- /dev/null +++ b/Project02/Project02.pro @@ -0,0 +1,31 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/Project02/main.cpp b/Project02/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/Project02/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/Project02/mainwindow.cpp b/Project02/mainwindow.cpp new file mode 100644 index 0000000..41a26bd --- /dev/null +++ b/Project02/mainwindow.cpp @@ -0,0 +1,15 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + diff --git a/Project02/mainwindow.h b/Project02/mainwindow.h new file mode 100644 index 0000000..4643e32 --- /dev/null +++ b/Project02/mainwindow.h @@ -0,0 +1,21 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; +#endif // MAINWINDOW_H diff --git a/Project02/mainwindow.ui b/Project02/mainwindow.ui new file mode 100644 index 0000000..b232854 --- /dev/null +++ b/Project02/mainwindow.ui @@ -0,0 +1,22 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + + + diff --git a/qtdemo03/delayform2.cpp b/qtdemo03/delayform2.cpp new file mode 100644 index 0000000..89bf3ec --- /dev/null +++ b/qtdemo03/delayform2.cpp @@ -0,0 +1,36 @@ +#include "delayform2.h" +#include "ui_delayform2.h" + +DelayForm2::DelayForm2(QWidget *parent) : + QWidget(parent), + ui(new Ui::DelayForm2) +{ + ui->setupUi(this); + ui->lcdNumber->display(5); + timer = new QTimer(this); + + ui->imageLabel->setScaledContents(true); + + connect(timer,&QTimer::timeout,[&]{ + int n = ui->lcdNumber->intValue(); + if(n == 0){ + timer->stop(); + // ui->imageLabel->setPixmap(QPixmap(":/images/images/butterfly.png")); + return; + } + ui->lcdNumber->display(--n); + }); + + + timer->start(1000); + + // 延迟 10 秒 + QTimer::singleShot(5000,[&]{ + ui->imageLabel->setPixmap(QPixmap(":/images/images/butterfly.png")); + }); +} + +DelayForm2::~DelayForm2() +{ + delete ui; +} diff --git a/qtdemo03/delayform2.h b/qtdemo03/delayform2.h new file mode 100644 index 0000000..81a9ebb --- /dev/null +++ b/qtdemo03/delayform2.h @@ -0,0 +1,26 @@ +#ifndef DELAYFORM2_H +#define DELAYFORM2_H + +#include +#include +#include + +namespace Ui { +class DelayForm2; +} + +class DelayForm2 : public QWidget +{ + Q_OBJECT + + QTimer *timer; + +public: + explicit DelayForm2(QWidget *parent = nullptr); + ~DelayForm2(); + +private: + Ui::DelayForm2 *ui; +}; + +#endif // DELAYFORM2_H diff --git a/qtdemo03/delayform2.ui b/qtdemo03/delayform2.ui new file mode 100644 index 0000000..8878b31 --- /dev/null +++ b/qtdemo03/delayform2.ui @@ -0,0 +1,117 @@ + + + DelayForm2 + + + + 0 + 0 + 640 + 480 + + + + Form + + + + + 60 + 10 + 547 + 81 + + + + + + + + + + 16777215 + 16777215 + + + + + -1 + 0 + + + + color: rgb(255, 85, 0); +background-color: rgb(170, 255, 255); +font: 75 11pt "Agency FB"; + + + QLCDNumber::Filled + + + + + + + 倒计时 + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 200 + 20 + + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 200 + 20 + + + + + + + + + + + + 130 + 90 + 371 + 381 + + + + + + + TextLabel + + + + + + + + + diff --git a/qtdemo03/form7.cpp b/qtdemo03/form7.cpp index a6e4039..6c03b0f 100644 --- a/qtdemo03/form7.cpp +++ b/qtdemo03/form7.cpp @@ -30,6 +30,11 @@ void Form7::on_btn3_clicked() ui->stackedWidget->setCurrentIndex(2); } +void Form7::on_btn4_clicked() +{ + ui->stackedWidget->setCurrentIndex(6); +} + void Form7::on_btn5_clicked() { ui->stackedWidget->setCurrentIndex(3); @@ -45,12 +50,12 @@ void Form7::on_btn7_clicked() ui->stackedWidget->setCurrentIndex(5); } -void Form7::on_btn4_clicked() -{ - ui->stackedWidget->setCurrentIndex(6); -} - void Form7::on_btn8_clicked() { ui->stackedWidget->setCurrentIndex(7); } + +void Form7::on_btn9_clicked() +{ + ui->stackedWidget->setCurrentIndex(8); +} diff --git a/qtdemo03/form7.h b/qtdemo03/form7.h index d2967e2..6e448f5 100644 --- a/qtdemo03/form7.h +++ b/qtdemo03/form7.h @@ -32,6 +32,8 @@ private slots: void on_btn8_clicked(); + void on_btn9_clicked(); + private: Ui::Form7 *ui; }; diff --git a/qtdemo03/form7.ui b/qtdemo03/form7.ui index b185d4c..c1bd51a 100644 --- a/qtdemo03/form7.ui +++ b/qtdemo03/form7.ui @@ -17,7 +17,7 @@ - 7 + 9 @@ -27,6 +27,8 @@ + + @@ -88,6 +90,20 @@ + + + + TimerForm1 + + + + + + + DelayForm2 + + + @@ -142,6 +158,18 @@
form8.h
1 + + TimerForm1 + QWidget +
timerform1.h
+ 1 +
+ + DelayForm2 + QWidget +
delayform2.h
+ 1 +
diff --git a/qtdemo03/form8.cpp b/qtdemo03/form8.cpp index def9dce..43a71c7 100644 --- a/qtdemo03/form8.cpp +++ b/qtdemo03/form8.cpp @@ -20,15 +20,17 @@ Form8::~Form8() bool Form8::event(QEvent *evt) { qDebug()<<"事件分发"<type(); - if(evt->type() == QEvent::KeyPress){ + if(evt->type() == QEvent::KeyRelease){ QKeyEvent *keyEvent = static_cast(evt); - if(keyEvent->key() == Qt::Key_Return){ + if(keyEvent->key() == Qt::Key_Tab||keyEvent->key() == Qt::Key_O){ // ui->Form8label->setText(tr("按下 %1 键").arg(keyEvent->text())); qDebug()<text()); + return true; } // return false; // 不分发此事件 } - return QWidget::event(evt); +// return QWidget::event(evt); + return false; } void Form8::keyPressEvent(QKeyEvent *event) diff --git a/qtdemo03/form8.h b/qtdemo03/form8.h index 826e31e..8becbc3 100644 --- a/qtdemo03/form8.h +++ b/qtdemo03/form8.h @@ -19,8 +19,8 @@ public: ~Form8(); protected: - virtual void keyPressEvent(QKeyEvent *event) override; - virtual bool event(QEvent *event) override; + virtual void keyPressEvent(QKeyEvent *event); + virtual bool event(QEvent *event); private: Ui::Form8 *ui; diff --git a/qtdemo03/main.cpp b/qtdemo03/main.cpp index 1aed7e9..16d9317 100644 --- a/qtdemo03/main.cpp +++ b/qtdemo03/main.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include @@ -22,8 +24,10 @@ int main(int argc, char *argv[]) // Form5 w; // Form6 w; // SmallWidget w; -// Form7 w; Form7 w; +// Form8 w; +// TimerForm1 w; +// DelayForm2 w; w.show(); return a.exec(); } diff --git a/qtdemo03/qtdemo03.pro b/qtdemo03/qtdemo03.pro index fef1604..dfbb608 100644 --- a/qtdemo03/qtdemo03.pro +++ b/qtdemo03/qtdemo03.pro @@ -16,6 +16,7 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + delayform2.cpp \ form1.cpp \ form2.cpp \ form3.cpp \ @@ -26,9 +27,12 @@ SOURCES += \ form8.cpp \ main.cpp \ smallwidget.cpp \ - mainwindow.cpp + mainwindow.cpp \ + timerform1.cpp + HEADERS += \ + delayform2.h \ form1.h \ form2.h \ form3.h \ @@ -38,9 +42,11 @@ HEADERS += \ form7.h \ form8.h \ smallwidget.h \ - mainwindow.h + mainwindow.h \ + timerform1.h FORMS += \ + delayform2.ui \ form1.ui \ form2.ui \ form3.ui \ @@ -49,7 +55,8 @@ FORMS += \ form6.ui \ form7.ui \ form8.ui \ - mainwindow.ui + mainwindow.ui \ + timerform1.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin diff --git a/qtdemo03/timerform1.cpp b/qtdemo03/timerform1.cpp new file mode 100644 index 0000000..bd2d513 --- /dev/null +++ b/qtdemo03/timerform1.cpp @@ -0,0 +1,54 @@ +#include "timerform1.h" +#include "ui_timerform1.h" + +TimerForm1::TimerForm1(QWidget *parent) : + QWidget(parent), + ui(new Ui::TimerForm1) +{ + ui->setupUi(this); + ui->stoptimebtn->setEnabled(false); // 将停止按钮禁用 + + // 手动实现 + timer = new QTimer(this); + // 绑定 timeout 超时信号 + connect(timer,&QTimer::timeout,[&]{ + QDateTime currentTime = QDateTime::currentDateTime(); + ui->datetimeLabel->setText(currentTime.toString("yyyy-MM-dd hh:mm:ss")); // 将当前时间显示在 QLabel 上 + }); +} + +TimerForm1::~TimerForm1() +{ + delete ui; +} + +void TimerForm1::on_starttimebtn_clicked() +{ + // timerId = startTimer(1000); // 启动定时器,每秒触发一次定时器事件 + + // 手动实现 + timer->start(1000); + ui->starttimebtn->setDisabled(true); // 将开始按钮禁用 + ui->stoptimebtn->setEnabled(true); // 启用停止按钮 + +} + +void TimerForm1::on_stoptimebtn_clicked() +{ + // killTimer(timerId); // 停止定时器 + + // 手动实现 + if(timer->isActive()) + timer->stop(); + ui->stoptimebtn->setEnabled(false); // 将停止按钮禁用 + ui->starttimebtn->setDisabled(false); // 启用开始按钮 + +} + +void TimerForm1::timerEvent(QTimerEvent *e){ + // 发生了定时器事件 + // 获取当前时间 + QDateTime currentTime = QDateTime::currentDateTime(); + ui->datetimeLabel->setText(currentTime.toString("yyyy-MM-dd hh:mm:ss")); // 将当前时间显示在 QLabel 上 +} + diff --git a/qtdemo03/timerform1.h b/qtdemo03/timerform1.h new file mode 100644 index 0000000..76525d4 --- /dev/null +++ b/qtdemo03/timerform1.h @@ -0,0 +1,34 @@ +#ifndef TIMERFORM1_H +#define TIMERFORM1_H + +#include +#include +#include + +namespace Ui { +class TimerForm1; +} + +class TimerForm1 : public QWidget +{ + Q_OBJECT + int timerId; + QTimer *timer; + +public: + explicit TimerForm1(QWidget *parent = nullptr); + ~TimerForm1(); + +protected: + virtual void timerEvent(QTimerEvent *event) override; + +private slots: + void on_starttimebtn_clicked(); + + void on_stoptimebtn_clicked(); + +private: + Ui::TimerForm1 *ui; +}; + +#endif // TIMERFORM1_H diff --git a/qtdemo03/timerform1.ui b/qtdemo03/timerform1.ui new file mode 100644 index 0000000..b446834 --- /dev/null +++ b/qtdemo03/timerform1.ui @@ -0,0 +1,81 @@ + + + TimerForm1 + + + + 0 + 0 + 640 + 480 + + + + Form + + + + + + Qt::Horizontal + + + + 332 + 425 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 200 + 425 + + + + + + + + 停止定时器 + + + + + + + TextLabel + + + + + + + 启动定时器 + + + + + + + +