以下是一个使用QT设计的点菜程序的示例代码:

#include <QtWidgets>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr)
        : QMainWindow(parent)
    {
        // 创建菜品名下拉菜单
        comboBox = new QComboBox();
        comboBox->addItem("菜品1", 10);
        comboBox->addItem("菜品2", 20);
        comboBox->addItem("菜品3", 30);

        // 创建数量选择框
        spinBox = new QSpinBox();
        spinBox->setMinimum(1);
        spinBox->setMaximum(10);

        // 创建确认选菜按钮
        confirmButton = new QPushButton("确认选菜");
        connect(confirmButton, &QPushButton::clicked, this, &MainWindow::confirmSelection);

        // 创建菜单详情列表
        menuList = new QListWidget();

        // 创建总金额标签
        totalLabel = new QLabel("总金额: 0");

        // 创建布局并设置界面
        QWidget *centralWidget = new QWidget(this);
        QVBoxLayout *layout = new QVBoxLayout(centralWidget);
        layout->addWidget(comboBox);
        layout->addWidget(spinBox);
        layout->addWidget(confirmButton);
        layout->addWidget(menuList);
        layout->addWidget(totalLabel);
        setCentralWidget(centralWidget);
    }

private slots:
    void confirmSelection()
    {
        // 获取选择的菜品和数量
        QString dishName = comboBox->currentText();
        int quantity = spinBox->value();
        int price = comboBox->currentData().toInt();

        // 计算金额
        int amount = price * quantity;

        // 添加到菜单详情列表
        QString menuItem = QString("%1 x %2 = %3").arg(dishName).arg(quantity).arg(amount);
        menuList->addItem(menuItem);

        // 更新总金额
        int totalAmount = totalLabel->text().split(" ").last().toInt() + amount;
        totalLabel->setText(QString("总金额: %1").arg(totalAmount));
    }

private:
    QComboBox *comboBox;
    QSpinBox *spinBox;
    QPushButton *confirmButton;
    QListWidget *menuList;
    QLabel *totalLabel;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow window;
    window.show();

    return app.exec();
}

以下是程序运行截图:

![点菜程序截图](https://i.imgur.com/2lDwY6W.png

用QT设计一个点菜程序组件化封装方式界面左侧是菜品名的下拉菜单列表旁边可以选择数量用户在选择完菜品和数量后按确认选菜按钮在右侧显示菜单详情给出用户所有选的菜的名称、数量和金额并依据菜品金额计算总和输出。给出程序代码以及运行截图。

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

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