day4: 基于day3,添加Timer案例

This commit is contained in:
flykhan 2023-08-10 12:05:02 +08:00
parent cd4484ebe5
commit 0a0afb1a0d
19 changed files with 584 additions and 15 deletions

73
Project02/.gitignore vendored Normal file
View File

@ -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

31
Project02/Project02.pro Normal file
View File

@ -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

11
Project02/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

15
Project02/mainwindow.cpp Normal file
View File

@ -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;
}

21
Project02/mainwindow.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
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

22
Project02/mainwindow.ui Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar"/>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

36
qtdemo03/delayform2.cpp Normal file
View File

@ -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;
}

26
qtdemo03/delayform2.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef DELAYFORM2_H
#define DELAYFORM2_H
#include <QWidget>
#include <QTimer>
#include <QPixmap>
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

117
qtdemo03/delayform2.ui Normal file
View File

@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DelayForm2</class>
<widget class="QWidget" name="DelayForm2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>60</x>
<y>10</y>
<width>547</width>
<height>81</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="2">
<widget class="QLCDNumber" name="lcdNumber">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="baseSize">
<size>
<width>-1</width>
<height>0</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">color: rgb(255, 85, 0);
background-color: rgb(170, 255, 255);
font: 75 11pt &quot;Agency FB&quot;;</string>
</property>
<property name="segmentStyle">
<enum>QLCDNumber::Filled</enum>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label">
<property name="text">
<string>倒计时</string>
</property>
</widget>
</item>
<item row="0" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>200</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="3">
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>200</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="widget_2" native="true">
<property name="geometry">
<rect>
<x>130</x>
<y>90</y>
<width>371</width>
<height>381</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="imageLabel">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -30,6 +30,11 @@ void Form7::on_btn3_clicked()
ui->stackedWidget->setCurrentIndex(2); ui->stackedWidget->setCurrentIndex(2);
} }
void Form7::on_btn4_clicked()
{
ui->stackedWidget->setCurrentIndex(6);
}
void Form7::on_btn5_clicked() void Form7::on_btn5_clicked()
{ {
ui->stackedWidget->setCurrentIndex(3); ui->stackedWidget->setCurrentIndex(3);
@ -45,12 +50,12 @@ void Form7::on_btn7_clicked()
ui->stackedWidget->setCurrentIndex(5); ui->stackedWidget->setCurrentIndex(5);
} }
void Form7::on_btn4_clicked()
{
ui->stackedWidget->setCurrentIndex(6);
}
void Form7::on_btn8_clicked() void Form7::on_btn8_clicked()
{ {
ui->stackedWidget->setCurrentIndex(7); ui->stackedWidget->setCurrentIndex(7);
} }
void Form7::on_btn9_clicked()
{
ui->stackedWidget->setCurrentIndex(8);
}

View File

@ -32,6 +32,8 @@ private slots:
void on_btn8_clicked(); void on_btn8_clicked();
void on_btn9_clicked();
private: private:
Ui::Form7 *ui; Ui::Form7 *ui;
}; };

View File

@ -17,7 +17,7 @@
<item row="0" column="0"> <item row="0" column="0">
<widget class="QStackedWidget" name="stackedWidget"> <widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex"> <property name="currentIndex">
<number>7</number> <number>9</number>
</property> </property>
<widget class="Form1" name="page_1"/> <widget class="Form1" name="page_1"/>
<widget class="Form2" name="page_2"/> <widget class="Form2" name="page_2"/>
@ -27,6 +27,8 @@
<widget class="Form6" name="page_6"/> <widget class="Form6" name="page_6"/>
<widget class="SmallWidget" name="page_SmallWidget"/> <widget class="SmallWidget" name="page_SmallWidget"/>
<widget class="Form8" name="page_8"/> <widget class="Form8" name="page_8"/>
<widget class="TimerForm1" name="page_timerForm1"/>
<widget class="DelayForm2" name="page_delayForm2"/>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
@ -88,6 +90,20 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="btn9">
<property name="text">
<string>TimerForm1</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn10">
<property name="text">
<string>DelayForm2</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>
@ -142,6 +158,18 @@
<header>form8.h</header> <header>form8.h</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
<customwidget>
<class>TimerForm1</class>
<extends>QWidget</extends>
<header>timerform1.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>DelayForm2</class>
<extends>QWidget</extends>
<header>delayform2.h</header>
<container>1</container>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections/> <connections/>

View File

@ -20,15 +20,17 @@ Form8::~Form8()
bool Form8::event(QEvent *evt) bool Form8::event(QEvent *evt)
{ {
qDebug()<<"事件分发"<<evt->type(); qDebug()<<"事件分发"<<evt->type();
if(evt->type() == QEvent::KeyPress){ if(evt->type() == QEvent::KeyRelease){
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(evt); QKeyEvent *keyEvent = static_cast<QKeyEvent*>(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())); // ui->Form8label->setText(tr("按下 %1 键").arg(keyEvent->text()));
qDebug()<<tr("按下 %1 键").arg(keyEvent->text()); qDebug()<<tr("按下 %1 键").arg(keyEvent->text());
return true;
} }
// return false; // 不分发此事件 // return false; // 不分发此事件
} }
return QWidget::event(evt); // return QWidget::event(evt);
return false;
} }
void Form8::keyPressEvent(QKeyEvent *event) void Form8::keyPressEvent(QKeyEvent *event)

View File

@ -19,8 +19,8 @@ public:
~Form8(); ~Form8();
protected: protected:
virtual void keyPressEvent(QKeyEvent *event) override; virtual void keyPressEvent(QKeyEvent *event);
virtual bool event(QEvent *event) override; virtual bool event(QEvent *event);
private: private:
Ui::Form8 *ui; Ui::Form8 *ui;

View File

@ -8,6 +8,8 @@
#include <form7.h> #include <form7.h>
#include <smallwidget.h> #include <smallwidget.h>
#include <form8.h> #include <form8.h>
#include <timerform1.h>
#include <delayform2.h>
#include <QApplication> #include <QApplication>
@ -22,8 +24,10 @@ int main(int argc, char *argv[])
// Form5 w; // Form5 w;
// Form6 w; // Form6 w;
// SmallWidget w; // SmallWidget w;
// Form7 w;
Form7 w; Form7 w;
// Form8 w;
// TimerForm1 w;
// DelayForm2 w;
w.show(); w.show();
return a.exec(); return a.exec();
} }

View File

@ -16,6 +16,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \ SOURCES += \
delayform2.cpp \
form1.cpp \ form1.cpp \
form2.cpp \ form2.cpp \
form3.cpp \ form3.cpp \
@ -26,9 +27,12 @@ SOURCES += \
form8.cpp \ form8.cpp \
main.cpp \ main.cpp \
smallwidget.cpp \ smallwidget.cpp \
mainwindow.cpp mainwindow.cpp \
timerform1.cpp
HEADERS += \ HEADERS += \
delayform2.h \
form1.h \ form1.h \
form2.h \ form2.h \
form3.h \ form3.h \
@ -38,9 +42,11 @@ HEADERS += \
form7.h \ form7.h \
form8.h \ form8.h \
smallwidget.h \ smallwidget.h \
mainwindow.h mainwindow.h \
timerform1.h
FORMS += \ FORMS += \
delayform2.ui \
form1.ui \ form1.ui \
form2.ui \ form2.ui \
form3.ui \ form3.ui \
@ -49,7 +55,8 @@ FORMS += \
form6.ui \ form6.ui \
form7.ui \ form7.ui \
form8.ui \ form8.ui \
mainwindow.ui mainwindow.ui \
timerform1.ui
# Default rules for deployment. # Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin qnx: target.path = /tmp/$${TARGET}/bin

54
qtdemo03/timerform1.cpp Normal file
View File

@ -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 上
}

34
qtdemo03/timerform1.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef TIMERFORM1_H
#define TIMERFORM1_H
#include <QWidget>
#include <QDateTime>
#include <QTimer>
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

81
qtdemo03/timerform1.ui Normal file
View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TimerForm1</class>
<widget class="QWidget" name="TimerForm1">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1" colspan="3">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>332</width>
<height>425</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="4">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>200</width>
<height>425</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="4">
<widget class="QPushButton" name="stoptimebtn">
<property name="text">
<string>停止定时器</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="datetimeLabel">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item row="1" column="2" colspan="2">
<widget class="QPushButton" name="starttimebtn">
<property name="text">
<string>启动定时器</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>