day11 coding Qt案例: 热了与冷饮

This commit is contained in:
flykhan 2023-06-27 15:43:16 +08:00
parent e7d3739038
commit 1c2e268a52
8 changed files with 199 additions and 0 deletions

11
day11/qtdemo1/main.cpp Executable file
View File

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

33
day11/qtdemo1/qtdemo1.pro Executable file
View File

@ -0,0 +1,33 @@
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 \
student.cpp \
teacher.cpp \
widget.cpp
HEADERS += \
student.h \
teacher.h \
widget.h
TARGET = moneyorno
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

11
day11/qtdemo1/student.cpp Executable file
View File

@ -0,0 +1,11 @@
#include "student.h"
Student::Student(QWidget *parent) : QWidget(parent)
{
}
void Student::drink()
{
qDebug() << "请老师吃冷饮" << endl;
}

20
day11/qtdemo1/student.h Executable file
View File

@ -0,0 +1,20 @@
#ifndef STUDENT_H
#define STUDENT_H
#include <QWidget>
#include <QDebug>
class Student : public QWidget
{
Q_OBJECT
public:
explicit Student(QWidget *parent = nullptr);
public slots:
void drink();
signals:
};
#endif // STUDENT_H

6
day11/qtdemo1/teacher.cpp Executable file
View File

@ -0,0 +1,6 @@
#include "teacher.h"
Teacher::Teacher(QWidget *parent) : QWidget(parent)
{
}

17
day11/qtdemo1/teacher.h Executable file
View File

@ -0,0 +1,17 @@
#ifndef TEACHER_H
#define TEACHER_H
#include <QWidget>
class Teacher : public QWidget
{
Q_OBJECT
public:
explicit Teacher(QWidget *parent = nullptr);
signals:
void hot();
};
#endif // TEACHER_H

69
day11/qtdemo1/widget.cpp Executable file
View File

@ -0,0 +1,69 @@
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
resize(500,300);
setWindowTitle("热了与冷饮");
n=0;
btn = new QPushButton("热了",this);
btn->setFixedSize(100,50);
btn->move(100,100);
btn2 = new QPushButton("没钱了",this);
btn2->setFixedSize(100,50);
btn2->move(100,200);
lable1 = new QLabel("显示",this);
lable1->setFixedSize(200,50);
lable1->move(250,100);
lable2 = new QLabel("请客次数: ",this);
lable2->setFixedSize(200,50);
lable2->move(250,200);
t1 = new Teacher();
s1 = new Student();
connect(t1,&Teacher::hot,s1,&Student::drink);
connect(btn,&QPushButton::clicked,this,&Widget::handleHot);
connect(btn2,&QPushButton::clicked,this,&Widget::noMoney);
emit t1->hot();
}
void Widget::handleHot()
{
emit t1->hot();
Widget::print();
}
void Widget::print()
{
n++;
if(n>0){
if(n%2==0)
lable1->setText("请老师吃巧乐兹");
else
lable1->setText("请老师吃老冰棍");
lable2->setText("请客次数: "+QString::number(n));
}
}
void Widget::noMoney()
{
n=0;
lable1->setText("请老师喝西北风");
lable2->setText("请客次数: "+QString::number(n));
}
Widget::~Widget()
{
}

32
day11/qtdemo1/widget.h Executable file
View File

@ -0,0 +1,32 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include "teacher.h"
#include "student.h"
class Widget : public QWidget
{
Q_OBJECT
private:
Teacher *t1;
Student *s1;
int n;
private:
QPushButton *btn,*btn2;
QLabel *lable1,*lable2;
public slots:
void handleHot();
void print();
void noMoney();
public:
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H