用QT设计一个点菜程序组件化封装方式界面左侧是菜品名的下拉菜单列表旁边可以选择数量选择完按确认按钮。在右侧显示菜单详情菜单详情包括选择的菜品名称、菜品数量、菜品价格并给出金额总计。给出程序代码以及运行截图。
以下是一个简单的用QT设计的点菜程序的代码:
mainwindow.h文件:
#ifndef MAINWINDOW_H #define MAINWINDOW_H
#include
class MainWindow : public QMainWindow { Q_OBJECT
public: MainWindow(QWidget *parent = nullptr); ~MainWindow();
private slots: void updateTotalAmount(); void confirmOrder();
private: QComboBox *dishComboBox; QSpinBox *quantitySpinBox; QLabel *detailLabel; QLabel *totalAmountLabel; QPushButton *confirmButton; double totalAmount;
void setupUi();
};
#endif // MAINWINDOW_H
mainwindow.cpp文件:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setupUi(); }
MainWindow::~MainWindow() { }
void MainWindow::updateTotalAmount() { QString dishName = dishComboBox->currentText(); int quantity = quantitySpinBox->value(); double price = 0.0; // 根据菜品名称获取菜品价格的逻辑
double amount = quantity * price;
totalAmount += amount;
QString detailText = QString("菜品名称:%1\n菜品数量:%2\n菜品价格:%3\n")
.arg(dishName).arg(quantity).arg(price);
detailLabel->setText(detailText);
totalAmountLabel->setText(QString("金额总计:%1").arg(totalAmount));
}
void MainWindow::confirmOrder() { // 执行下单逻辑 }
void MainWindow::setupUi() { dishComboBox = new QComboBox(this); dishComboBox->addItem("菜品1"); dishComboBox->addItem("菜品2"); dishComboBox->addItem("菜品3");
quantitySpinBox = new QSpinBox(this);
quantitySpinBox->setMinimum(1);
quantitySpinBox->setMaximum(100);
detailLabel = new QLabel(this);
totalAmountLabel = new QLabel(this);
confirmButton = new QPushButton("确认", this);
connect(dishComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTotalAmount()));
connect(quantitySpinBox, SIGNAL(valueChanged(int)), this, SLOT(updateTotalAmount()));
connect(confirmButton, SIGNAL(clicked()), this, SLOT(confirmOrder()));
QWidget *centralWidget = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
QHBoxLayout *topLayout = new QHBoxLayout();
topLayout->addWidget(dishComboBox);
topLayout->addWidget(quantitySpinBox);
mainLayout->addLayout(topLayout);
mainLayout->addWidget(detailLabel);
mainLayout->addWidget(totalAmountLabel);
mainLayout->addWidget(confirmButton);
setCentralWidget(centralWidget);
}
main.cpp文件:
#include "mainwindow.h"
#include
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
运行截图
原文地址: https://www.cveoy.top/t/topic/irSN 著作权归作者所有。请勿转载和采集!