day11 coding Qt案例: 餐馆点餐

This commit is contained in:
flykhan 2023-06-27 20:45:52 +08:00
parent 1c2e268a52
commit 5bcb4c519d
6 changed files with 183 additions and 0 deletions

46
day11/qtdemo2/custom.cpp Executable file
View File

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

32
day11/qtdemo2/custom.h Executable file
View File

@ -0,0 +1,32 @@
#ifndef CUSTOM_H
#define CUSTOM_H
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QDebug>
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

26
day11/qtdemo2/main.cpp Executable file
View File

@ -0,0 +1,26 @@
#include "custom.h"
#include "restaurant.h"
#include <QApplication>
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();
}

30
day11/qtdemo2/qtdemo2.pro Executable file
View File

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

25
day11/qtdemo2/restaurant.cpp Executable file
View File

@ -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); // 单位: 毫秒
}

24
day11/qtdemo2/restaurant.h Executable file
View File

@ -0,0 +1,24 @@
#ifndef RESTAURANT_H
#define RESTAURANT_H
#include <QWidget>
#include <QTimer>
#include <QDebug>
#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