day1: homework 初始版本

This commit is contained in:
flykhan 2023-08-08 17:47:59 +08:00
parent 9522ecd007
commit 01c4a6e3ad
20 changed files with 527 additions and 0 deletions

View File

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

36
homework-day1/main.cpp Normal file
View File

@ -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 <QApplication>
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();
}

11
homework-day1/widget.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
}
Widget::~Widget()
{
}

14
homework-day1/widget.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H

15
homework-day1/widget1.cpp Normal file
View File

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

21
homework-day1/widget1.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef WIDGET1_H
#define WIDGET1_H
#include <QWidget>
#include <QPushButton>
#include <QDebug>
class Widget1 : public QWidget
{
Q_OBJECT
public:
explicit Widget1(QWidget *parent = nullptr);
public:
QPushButton *btn;
signals:
};
#endif // WIDGET1_H

19
homework-day1/widget2.cpp Normal file
View File

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

22
homework-day1/widget2.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef WIDGET2_H
#define WIDGET2_H
#include <QWidget>
#include <QLabel>
#include <QPushButton>
class Widget2 : public QWidget
{
Q_OBJECT
public:
explicit Widget2(QWidget *parent = nullptr);
public:
QPushButton *btn;
QLabel *label;
signals:
};
#endif // WIDGET2_H

22
homework-day1/widget3.cpp Normal file
View File

@ -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()<<str1+str2;
});
}

23
homework-day1/widget3.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef WIDGET3_H
#define WIDGET3_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QDebug>
class Widget3 : public QWidget
{
Q_OBJECT
public:
explicit Widget3(QWidget *parent = nullptr);
public:
QPushButton *btn;
QLineEdit *edit1,*edit2;
signals:
};
#endif // WIDGET3_H

27
homework-day1/widget4.cpp Normal file
View File

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

24
homework-day1/widget4.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef WIDGET4_H
#define WIDGET4_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
class Widget4 : public QWidget
{
Q_OBJECT
public:
explicit Widget4(QWidget *parent = nullptr);
private:
QPushButton *btn;
QLineEdit *edit1,*edit2;
QLabel *label;
signals:
};
#endif // WIDGET4_H

26
homework-day1/widget5.cpp Normal file
View File

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

23
homework-day1/widget5.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef WIDGET5_H
#define WIDGET5_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#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

18
homework-day1/widget6.cpp Normal file
View File

@ -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(); // 发送隐身信号
});
}

22
homework-day1/widget6.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef WIDGET6_H
#define WIDGET6_H
#include <QWidget>
#include <QPushButton>
class Widget6 : public QWidget
{
Q_OBJECT
public:
explicit Widget6(QWidget *parent = nullptr);
public:
QPushButton *btn; // 隐身信号按钮
signals:
void stealth(); // 隐身
};
#endif // WIDGET6_H

79
homework-day1/widget7.cpp Normal file
View File

@ -0,0 +1,79 @@
#include "widget7.h"
#include <QDebug>
Widget7::Widget7(QWidget *parent) : QWidget(parent)
{
setWindowTitle("题目6");
setFixedSize(800,600);
vct = new QVector<QString>();
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<QString>::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()<<str;
vct->append(str); // 向 QVector 中添加学生
}

31
homework-day1/widget7.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef WIDGET7_H
#define WIDGET7_H
#include <QWidget>
#include <QPushButton>
#include <QVector>
class Widget7 : public QWidget
{
Q_OBJECT
public:
explicit Widget7(QWidget *parent = nullptr);
~Widget7()
{
delete vct;
}
private:
QVector<QString> *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

26
homework-day1/widget8.cpp Normal file
View File

@ -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()<<stu;
}
// 清空数据,准备下一轮评选
selectedStudents.clear();
judgeCount = 0;
}
}

24
homework-day1/widget8.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef WIDGET8_H
#define WIDGET8_H
#include <QWidget>
#include <QSet>
#include <QDebug>
class Widget8 : public QWidget
{
Q_OBJECT
public:
explicit Widget8(QWidget *parent = nullptr);
private:
QSet<QString> selectedStudents;
int judgeCount;
public slots:
void receviceStudentSelection(const QString &stuName);
};
#endif // WIDGET8_H