From 01c4a6e3ad6242ab1403ea8e8c91001a25bb3afb Mon Sep 17 00:00:00 2001 From: flykhan Date: Tue, 8 Aug 2023 17:47:59 +0800 Subject: [PATCH] =?UTF-8?q?day1:=20homework=20=E5=88=9D=E5=A7=8B=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework-day1/homework-day1.pro | 44 ++++++++++++++++++ homework-day1/main.cpp | 36 +++++++++++++++ homework-day1/widget.cpp | 11 +++++ homework-day1/widget.h | 14 ++++++ homework-day1/widget1.cpp | 15 +++++++ homework-day1/widget1.h | 21 +++++++++ homework-day1/widget2.cpp | 19 ++++++++ homework-day1/widget2.h | 22 +++++++++ homework-day1/widget3.cpp | 22 +++++++++ homework-day1/widget3.h | 23 ++++++++++ homework-day1/widget4.cpp | 27 +++++++++++ homework-day1/widget4.h | 24 ++++++++++ homework-day1/widget5.cpp | 26 +++++++++++ homework-day1/widget5.h | 23 ++++++++++ homework-day1/widget6.cpp | 18 ++++++++ homework-day1/widget6.h | 22 +++++++++ homework-day1/widget7.cpp | 79 +++++++++++++++++++++++++++++++++ homework-day1/widget7.h | 31 +++++++++++++ homework-day1/widget8.cpp | 26 +++++++++++ homework-day1/widget8.h | 24 ++++++++++ 20 files changed, 527 insertions(+) create mode 100644 homework-day1/homework-day1.pro create mode 100644 homework-day1/main.cpp create mode 100644 homework-day1/widget.cpp create mode 100644 homework-day1/widget.h create mode 100644 homework-day1/widget1.cpp create mode 100644 homework-day1/widget1.h create mode 100644 homework-day1/widget2.cpp create mode 100644 homework-day1/widget2.h create mode 100644 homework-day1/widget3.cpp create mode 100644 homework-day1/widget3.h create mode 100644 homework-day1/widget4.cpp create mode 100644 homework-day1/widget4.h create mode 100644 homework-day1/widget5.cpp create mode 100644 homework-day1/widget5.h create mode 100644 homework-day1/widget6.cpp create mode 100644 homework-day1/widget6.h create mode 100644 homework-day1/widget7.cpp create mode 100644 homework-day1/widget7.h create mode 100644 homework-day1/widget8.cpp create mode 100644 homework-day1/widget8.h diff --git a/homework-day1/homework-day1.pro b/homework-day1/homework-day1.pro new file mode 100644 index 0000000..4f144b7 --- /dev/null +++ b/homework-day1/homework-day1.pro @@ -0,0 +1,44 @@ +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 \ + widget.cpp \ + widget1.cpp \ + widget2.cpp \ + widget3.cpp \ + widget4.cpp \ + widget5.cpp \ + widget6.cpp \ + widget7.cpp \ + widget8.cpp + +HEADERS += \ + widget.h \ + widget1.h \ + widget2.h \ + widget3.h \ + widget4.h \ + widget5.h \ + widget6.h \ + widget7.h \ + widget8.h + +# 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/homework-day1/main.cpp b/homework-day1/main.cpp new file mode 100644 index 0000000..78725e7 --- /dev/null +++ b/homework-day1/main.cpp @@ -0,0 +1,36 @@ +#include "widget.h" +#include "widget1.h" // 题目1 +#include "widget2.h" // 题目2 +#include "widget3.h" // 题目3 +#include "widget4.h" // 题目4 +#include "widget5.h" // 题目5 +#include "widget7.h" // 题目6,题目7 +#include "widget8.h" // 题目7 + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + // Widget7 w; + // w.show(); + Widget7 judge1; + Widget7 judge2; + Widget7 judge3; + + Widget8 w8; + + judge1.show(); + judge2.show(); + judge3.show(); + + // 建立信号槽连接 + w8.connect(&judge1,&Widget7::studentSelected,&w8,&Widget8::receviceStudentSelection); + w8.connect(&judge2,&Widget7::studentSelected,&w8,&Widget8::receviceStudentSelection); + w8.connect(&judge2,&Widget7::studentSelected,&w8,&Widget8::receviceStudentSelection); + + +// w8.show(); + + return a.exec(); +} diff --git a/homework-day1/widget.cpp b/homework-day1/widget.cpp new file mode 100644 index 0000000..7350ff4 --- /dev/null +++ b/homework-day1/widget.cpp @@ -0,0 +1,11 @@ +#include "widget.h" + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ +} + +Widget::~Widget() +{ +} + diff --git a/homework-day1/widget.h b/homework-day1/widget.h new file mode 100644 index 0000000..47b6270 --- /dev/null +++ b/homework-day1/widget.h @@ -0,0 +1,14 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); +}; +#endif // WIDGET_H diff --git a/homework-day1/widget1.cpp b/homework-day1/widget1.cpp new file mode 100644 index 0000000..c0634f9 --- /dev/null +++ b/homework-day1/widget1.cpp @@ -0,0 +1,15 @@ +#include "widget1.h" + +Widget1::Widget1(QWidget *parent) : QWidget(parent) +{ + setWindowTitle("题目1"); + setFixedSize(800,600); + + btn = new QPushButton("按钮",this); + btn->setFixedSize(200,100); + btn->move(10,10); + + connect(btn,&QPushButton::clicked,[&]()->void{ + qDebug()<<"I Love Qt5 Widget"; + }); +} diff --git a/homework-day1/widget1.h b/homework-day1/widget1.h new file mode 100644 index 0000000..9c1a302 --- /dev/null +++ b/homework-day1/widget1.h @@ -0,0 +1,21 @@ +#ifndef WIDGET1_H +#define WIDGET1_H + +#include +#include +#include + +class Widget1 : public QWidget +{ + Q_OBJECT +public: + explicit Widget1(QWidget *parent = nullptr); + +public: + QPushButton *btn; + +signals: + +}; + +#endif // WIDGET1_H diff --git a/homework-day1/widget2.cpp b/homework-day1/widget2.cpp new file mode 100644 index 0000000..f9e619b --- /dev/null +++ b/homework-day1/widget2.cpp @@ -0,0 +1,19 @@ +#include "widget2.h" + +Widget2::Widget2(QWidget *parent) : QWidget(parent) +{ + setWindowTitle("题目2"); + setFixedSize(800,600); + + btn = new QPushButton("按钮",this); + btn->setFixedSize(200,100); + btn->move(10,10); + + label = new QLabel("占位",this); + label->resize(300,200); + label->move(10,130); + + connect(btn,&QPushButton::clicked,[&]()->void{ + label->setText("1+1=2"); + }); +} diff --git a/homework-day1/widget2.h b/homework-day1/widget2.h new file mode 100644 index 0000000..c1ada41 --- /dev/null +++ b/homework-day1/widget2.h @@ -0,0 +1,22 @@ +#ifndef WIDGET2_H +#define WIDGET2_H + +#include +#include +#include + +class Widget2 : public QWidget +{ + Q_OBJECT +public: + explicit Widget2(QWidget *parent = nullptr); + +public: + QPushButton *btn; + QLabel *label; + +signals: + +}; + +#endif // WIDGET2_H diff --git a/homework-day1/widget3.cpp b/homework-day1/widget3.cpp new file mode 100644 index 0000000..2991706 --- /dev/null +++ b/homework-day1/widget3.cpp @@ -0,0 +1,22 @@ +#include "widget3.h" + +Widget3::Widget3(QWidget *parent) : QWidget(parent) +{ + setWindowTitle("题目3"); + setFixedSize(800,600); + + btn = new QPushButton("按钮",this); + btn->setFixedSize(200,100); + btn->move(10,10); + + edit1 = new QLineEdit(this); + edit1->move(10,120); + edit2 = new QLineEdit(this); + edit2->move(10,160); + + connect(btn,&QPushButton::clicked,[&]()->void{ + QString str1 = edit1->text(); + QString str2 = edit2->text(); + qDebug()< +#include +#include +#include + +class Widget3 : public QWidget +{ + Q_OBJECT +public: + explicit Widget3(QWidget *parent = nullptr); + +public: + QPushButton *btn; + QLineEdit *edit1,*edit2; + +signals: + +}; + +#endif // WIDGET3_H diff --git a/homework-day1/widget4.cpp b/homework-day1/widget4.cpp new file mode 100644 index 0000000..caf89cc --- /dev/null +++ b/homework-day1/widget4.cpp @@ -0,0 +1,27 @@ +#include "widget4.h" + +Widget4::Widget4(QWidget *parent) : QWidget(parent) +{ + setWindowTitle("题目4"); + setFixedSize(800,600); + + btn = new QPushButton("按钮",this); + btn->setFixedSize(200,100); + btn->move(10,10); + + edit1 = new QLineEdit(this); + edit1->move(10,120); + edit2 = new QLineEdit(this); + edit2->move(10,160); + + label = new QLabel("占位",this); + label->move(300,140); + + connect(btn,&QPushButton::clicked,[&]()->void{ + QString str1 = edit1->text(); + QString str2 = edit2->text(); + int res_num = str1.toInt()+str2.toInt(); + + label->setNum(res_num); + }); +} diff --git a/homework-day1/widget4.h b/homework-day1/widget4.h new file mode 100644 index 0000000..027ec09 --- /dev/null +++ b/homework-day1/widget4.h @@ -0,0 +1,24 @@ +#ifndef WIDGET4_H +#define WIDGET4_H + +#include +#include +#include +#include + +class Widget4 : public QWidget +{ + Q_OBJECT +public: + explicit Widget4(QWidget *parent = nullptr); + +private: + QPushButton *btn; + QLineEdit *edit1,*edit2; + QLabel *label; + +signals: + +}; + +#endif // WIDGET4_H diff --git a/homework-day1/widget5.cpp b/homework-day1/widget5.cpp new file mode 100644 index 0000000..fae87ba --- /dev/null +++ b/homework-day1/widget5.cpp @@ -0,0 +1,26 @@ +#include "widget5.h" + +Widget5::Widget5(QWidget *parent) : QWidget(parent) +{ + setWindowTitle("让Widget6消失"); + setFixedSize(400,600); + move(400,300); + + w6 = new Widget6; + + btn = new QPushButton("显示Widget6按钮",this); + btn->setFixedSize(200,100); + btn->move(10,10); + + connect(btn,&QPushButton::clicked,[&]()->void{ + this->setWindowTitle("让Widget6消失"); + w6->show(); + }); + + connect(w6,&Widget6::stealth,[&]()->void{ + this->setWindowTitle("Winget6消失了"); + w6->close(); + }); + + +} diff --git a/homework-day1/widget5.h b/homework-day1/widget5.h new file mode 100644 index 0000000..ffd653c --- /dev/null +++ b/homework-day1/widget5.h @@ -0,0 +1,23 @@ +#ifndef WIDGET5_H +#define WIDGET5_H + +#include +#include +#include +#include "widget6.h" + +class Widget5 : public QWidget +{ + Q_OBJECT +public: + explicit Widget5(QWidget *parent = nullptr); + +private: + Widget6 *w6; // Widget6 类对象的指针成员 + QPushButton *btn; + +signals: + +}; + +#endif // WIDGET5_H diff --git a/homework-day1/widget6.cpp b/homework-day1/widget6.cpp new file mode 100644 index 0000000..1bab5f5 --- /dev/null +++ b/homework-day1/widget6.cpp @@ -0,0 +1,18 @@ +#include "widget6.h" + +Widget6::Widget6(QWidget *parent) : QWidget(parent) +{ + + setWindowTitle("题目5-widget6"); + setFixedSize(400,600); + move(900,300); + + btn = new QPushButton("隐身按钮",this); + btn->setFixedSize(200,100); + btn->move(10,10); + + + connect(btn,&QPushButton::clicked,[&]()->void{ + emit this->stealth(); // 发送隐身信号 + }); +} diff --git a/homework-day1/widget6.h b/homework-day1/widget6.h new file mode 100644 index 0000000..b38a40b --- /dev/null +++ b/homework-day1/widget6.h @@ -0,0 +1,22 @@ +#ifndef WIDGET6_H +#define WIDGET6_H + +#include +#include + +class Widget6 : public QWidget +{ + Q_OBJECT +public: + explicit Widget6(QWidget *parent = nullptr); + +public: + QPushButton *btn; // 隐身信号按钮 + + +signals: + void stealth(); // 隐身 + +}; + +#endif // WIDGET6_H diff --git a/homework-day1/widget7.cpp b/homework-day1/widget7.cpp new file mode 100644 index 0000000..83b0379 --- /dev/null +++ b/homework-day1/widget7.cpp @@ -0,0 +1,79 @@ +#include "widget7.h" +#include + +Widget7::Widget7(QWidget *parent) : QWidget(parent) +{ + setWindowTitle("题目6"); + setFixedSize(800,600); + + vct = new QVector(); + + btn1 = new QPushButton("张三",this); + btn1->move(10,20); + btn2 = new QPushButton("李四",this); + btn2->move(10,70); + btn3 = new QPushButton("王五",this); + btn3->move(10,120); + btn4 = new QPushButton("赵六",this); + btn4->move(10,170); + btn5 = new QPushButton("孙七",this); + btn5->move(10,220); + + btn_show = new QPushButton("打印",this); + btn_show->move(150,20); + btn_restart = new QPushButton("重新再来",this); + btn_restart->move(150,70); + + connect(this,&Widget7::my_signal,this,&Widget7::my_handler); + + + connect(btn1,&QPushButton::clicked,[&]{ + QString str = btn1->text(); + emit this->my_signal(str); + emit this->studentSelected(str); + }); + + connect(btn2,&QPushButton::clicked,[&]{ + QString str = btn2->text(); + emit this->my_signal(str); + emit this->studentSelected(str); + }); + + connect(btn3,&QPushButton::clicked,[&]{ + QString str = btn3->text(); + emit this->my_signal(str); + emit this->studentSelected(str); + }); + + connect(btn4,&QPushButton::clicked,[&]{ + QString str = btn4->text(); + emit this->my_signal(str); + emit this->studentSelected(str); + }); + + connect(btn5,&QPushButton::clicked,[&]{ + QString str = btn5->text(); + emit this->my_signal(str); + emit this->studentSelected(str); + }); + + // 打印学生 + connect(btn_show,&QPushButton::clicked,[&]()->void{ + QVector::iterator it = vct->begin(); + while(it != vct->end()){ + qDebug()<<*it; + it++; + } + }); + + connect(btn_restart,&QPushButton::clicked,[&]()->void{ + vct->clear(); // 清空容器 + }); +} + +void Widget7::my_handler(const QString &str) +{ + + // qDebug()<append(str); // 向 QVector 中添加学生 +} diff --git a/homework-day1/widget7.h b/homework-day1/widget7.h new file mode 100644 index 0000000..1753ce8 --- /dev/null +++ b/homework-day1/widget7.h @@ -0,0 +1,31 @@ +#ifndef WIDGET7_H +#define WIDGET7_H + +#include +#include +#include + +class Widget7 : public QWidget +{ + Q_OBJECT +public: + explicit Widget7(QWidget *parent = nullptr); + ~Widget7() + { + delete vct; + } +private: + QVector *vct; + QPushButton *btn1,*btn2,*btn3,*btn4,*btn5; + QPushButton *btn_show,*btn_restart; // 显示按钮,重来按钮 + +public slots: + void my_handler(const QString &str); + +signals: + void my_signal(const QString &str); + void studentSelected(const QString &stuName); // 学生选择信号 + +}; + +#endif // WIDGET7_H diff --git a/homework-day1/widget8.cpp b/homework-day1/widget8.cpp new file mode 100644 index 0000000..79a0a30 --- /dev/null +++ b/homework-day1/widget8.cpp @@ -0,0 +1,26 @@ +#include "widget8.h" + +Widget8::Widget8(QWidget *parent) : QWidget(parent) +{ + judgeCount = 0; +} + +void Widget8::receviceStudentSelection(const QString &stuName) +{ + selectedStudents.insert(stuName); + judgeCount++; + + if(judgeCount == 3) + { + // 所有评委选择完毕 + qDebug()<<"学生名单: "; + for(const QString &stu: selectedStudents) + { + qDebug()< +#include +#include + +class Widget8 : public QWidget +{ + Q_OBJECT +public: + explicit Widget8(QWidget *parent = nullptr); + +private: + QSet selectedStudents; + int judgeCount; + +public slots: + void receviceStudentSelection(const QString &stuName); + + +}; + +#endif // WIDGET8_H