From 5bcb4c519d06fbb9fb14ee7dadadec9b5ba8bbef Mon Sep 17 00:00:00 2001 From: flykhan Date: Tue, 27 Jun 2023 20:45:52 +0800 Subject: [PATCH] =?UTF-8?q?day11=20coding=20Qt=E6=A1=88=E4=BE=8B:=20?= =?UTF-8?q?=E9=A4=90=E9=A6=86=E7=82=B9=E9=A4=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day11/qtdemo2/custom.cpp | 46 ++++++++++++++++++++++++++++++++++++ day11/qtdemo2/custom.h | 32 +++++++++++++++++++++++++ day11/qtdemo2/main.cpp | 26 ++++++++++++++++++++ day11/qtdemo2/qtdemo2.pro | 30 +++++++++++++++++++++++ day11/qtdemo2/restaurant.cpp | 25 ++++++++++++++++++++ day11/qtdemo2/restaurant.h | 24 +++++++++++++++++++ 6 files changed, 183 insertions(+) create mode 100755 day11/qtdemo2/custom.cpp create mode 100755 day11/qtdemo2/custom.h create mode 100755 day11/qtdemo2/main.cpp create mode 100755 day11/qtdemo2/qtdemo2.pro create mode 100755 day11/qtdemo2/restaurant.cpp create mode 100755 day11/qtdemo2/restaurant.h diff --git a/day11/qtdemo2/custom.cpp b/day11/qtdemo2/custom.cpp new file mode 100755 index 0000000..e11aa54 --- /dev/null +++ b/day11/qtdemo2/custom.cpp @@ -0,0 +1,46 @@ +#include "custom.h" + +Custom::Custom(QWidget *parent) + : QWidget(parent) +{ + // 初始化窗口的控件 + nameEdit = new QLineEdit(this); + nameEdit->resize(200,40); + nameEdit->move(10,10); + + nEdit = new QLineEdit(this); + nEdit->resize(100,40); + nEdit->move(10,60); + + toOrderBtn = new QPushButton("下单",this); + toOrderBtn->resize(80,40); + toOrderBtn->move(10,110); + + eatStateLable = new QLabel("未开吃",this); + eatStateLable->resize(100,40); + eatStateLable->move(10,160); + + // 设置按钮的点击事件的处理函数 + connect(toOrderBtn,&QPushButton::clicked,this,&Custom::toOrderHandle); +} + +void Custom::toOrderHandle() +{ + eatStateLable->setText("等待上菜……"); + // 下单按钮被点击了 + qDebug() << "准备下单了"; + qDebug() << "你的菜品: "<< nameEdit->text() << ", 数量:" << nEdit->text(); + + // 向店家发送信号,信号中包含菜品名和数量 + emit toOrderSignal(nameEdit->text(), nEdit->text().toInt()); +} + +void Custom::okOrderHandle() +{ + eatStateLable->setText(nameEdit->text()+"开吃……"); +} + +Custom::~Custom() +{ +} + diff --git a/day11/qtdemo2/custom.h b/day11/qtdemo2/custom.h new file mode 100755 index 0000000..05dba8b --- /dev/null +++ b/day11/qtdemo2/custom.h @@ -0,0 +1,32 @@ +#ifndef CUSTOM_H +#define CUSTOM_H + +#include +#include +#include +#include +#include + +class Custom : public QWidget +{ + Q_OBJECT + +private: + QLineEdit *nameEdit; // 菜品 + QLineEdit *nEdit; // 数量 + QPushButton *toOrderBtn; // 下单 + QLabel *eatStateLable; // 开吃状态 + +public slots: + void toOrderHandle(); // 处理按钮的点击事件 + void okOrderHandle(); // 店家上菜信号的处理函数 + + +signals: + void toOrderSignal(QString name,int n); + +public: + Custom(QWidget *parent = nullptr); + ~Custom(); +}; +#endif // CUSTOM_H diff --git a/day11/qtdemo2/main.cpp b/day11/qtdemo2/main.cpp new file mode 100755 index 0000000..9add1ae --- /dev/null +++ b/day11/qtdemo2/main.cpp @@ -0,0 +1,26 @@ +#include "custom.h" +#include "restaurant.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Custom c; + c.setWindowTitle("点餐系统"); + c.resize(800,600); + + // 创建店类的对象 + Restaurant r; + + // 绑定客户下单信号处理函数 + void (Custom:: *toOrder)(QString, int) = &Custom::toOrderSignal; + void (Restaurant:: *receiveOrder)(QString, int) = &Restaurant::receiveOrder; + QObject::connect(&c,toOrder,&r,receiveOrder); + + // 绑定店家上菜信号和客户的槽函数 + QObject::connect(&r,&Restaurant::okOrder,&c,&Custom::okOrderHandle); + + c.show(); + return a.exec(); +} diff --git a/day11/qtdemo2/qtdemo2.pro b/day11/qtdemo2/qtdemo2.pro new file mode 100755 index 0000000..6001a8d --- /dev/null +++ b/day11/qtdemo2/qtdemo2.pro @@ -0,0 +1,30 @@ +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 \ + custom.cpp \ + restaurant.cpp + +HEADERS += \ + custom.h \ + restaurant.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/day11/qtdemo2/restaurant.cpp b/day11/qtdemo2/restaurant.cpp new file mode 100755 index 0000000..c2b443e --- /dev/null +++ b/day11/qtdemo2/restaurant.cpp @@ -0,0 +1,25 @@ +#include "restaurant.h" + +Restaurant::Restaurant(QWidget *parent) : QWidget(parent) +{ + +} + +void Restaurant::receiveOrder(QString name, int n) +{ + qDebug() << "店家已接收到菜单: " << name << ", 数量: " << n; + + // 等待 10 秒 + // QThread::msleep(10*1000); // 单位毫秒,UI阻塞 + + // 创建定时器,UI非阻塞 + QTimer *timer = new QTimer(); + // 绑定定时器的超时信号与 Lambda 表达式的匿名处理函数 + QTimer::connect(timer,&QTimer::timeout,[&](){ + qDebug() << "完成订单,上菜"; + // 发送上菜的信号 + emit okOrder(); + }); + timer->setSingleShot(true); // 单次使用 + timer->start(10*1000); // 单位: 毫秒 +} diff --git a/day11/qtdemo2/restaurant.h b/day11/qtdemo2/restaurant.h new file mode 100755 index 0000000..73eb3a0 --- /dev/null +++ b/day11/qtdemo2/restaurant.h @@ -0,0 +1,24 @@ +#ifndef RESTAURANT_H +#define RESTAURANT_H + +#include +#include +#include +#include "custom.h" + +class Restaurant : public QWidget +{ + Q_OBJECT +public: + explicit Restaurant(QWidget *parent = nullptr); + +public slots: + // 接单的处理槽函数 + void receiveOrder(QString name, int n); + +signals: + void okOrder(); // 完成菜品 + +}; + +#endif // RESTAURANT_H