以下是一个简单的用QT设计的点菜程序的代码:

mainwindow.h文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QComboBox>
#include <QSpinBox>
#include <QLabel>
#include <QPushButton>

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
菜品数量:%2
菜品价格:%3
')
            .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 <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

运行截图:

[图片]

代码说明:

  • 该程序使用QT框架进行设计,实现了一个简单的点菜系统。
  • 程序使用组件化封装方式,将界面元素封装成独立的组件,方便维护和扩展。
  • 界面左侧使用下拉菜单显示菜品名称,右侧使用标签显示选择的菜品详情和金额总计。
  • 数量选择使用自旋框实现,方便用户调整数量。
  • 确认按钮用于确认订单。

功能特点:

  • 可以选择不同的菜品。
  • 可以选择菜品的数量。
  • 可以查看选择的菜品详情和金额总计。
  • 可以确认订单。

代码示例:

  • mainwindow.h 文件定义了主窗口类,包括界面元素的定义和功能函数的声明。
  • mainwindow.cpp 文件实现了主窗口类的功能函数,包括界面元素的初始化、事件处理等。
  • main.cpp 文件是程序的入口点,负责创建应用程序对象和主窗口对象。

运行截图:

[图片]

总结:

该程序是一个简单的点菜系统示例,可以帮助学习使用QT进行GUI程序开发。该程序具有良好的组件化封装特性,方便维护和扩展。可以通过添加数据库操作、网络通信等功能来扩展该程序的功能。

扩展阅读:

  • QT官方文档:https://doc.qt.io/
  • QT教程:https://www.learncpp.com/cpp-tutorial/qt-gui-programming/
QT点菜程序设计:组件化封装,下拉菜单选择菜品,数量控制,金额统计

原文地址: https://www.cveoy.top/t/topic/p9tF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录