QT周五作业

发布时间:2024年01月13日
题目:实现简单水果的价格重量计算

点击一次水果重量+1? 自动计算总价

代码:
widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QListWidgetItem>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

public slots:
    void itemclickedFun(QListWidgetItem *p);
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QListWidgetItem *ppeach = new QListWidgetItem;
    ppeach->setIcon(QIcon(":/rs/桃子.png"));
    ppeach->setText("桃子 ¥ 10/kg");
    ui->listWidget->addItem(ppeach);

    QListWidgetItem *pmango = new QListWidgetItem;
    pmango->setIcon(QIcon(":/rs/芒果.png"));
    pmango->setText("芒果 ¥ 20/kg");
    ui->listWidget->addItem(pmango);

    QListWidgetItem *papple = new QListWidgetItem;
    papple->setIcon(QIcon(":/rs/苹果.png"));
    papple->setText("苹果 ¥ 15/kg");
    ui->listWidget->addItem(papple);

    QListWidgetItem *ppenapple = new QListWidgetItem;
    ppenapple->setIcon(QIcon(":/rs/菠萝.png"));
    ppenapple->setText("菠萝 ¥ 18/kg");
    ui->listWidget->addItem(ppenapple);

    QListWidgetItem *pbanana = new QListWidgetItem;
    pbanana->setIcon(QIcon(":/rs/香蕉.png"));
    pbanana->setText("香蕉 ¥ 8/kg");
    ui->listWidget->addItem(pbanana);

    QListWidgetItem *pwatermelon = new QListWidgetItem;
    pwatermelon->setIcon(QIcon(":/rs/西瓜.png"));
    pwatermelon->setText("西瓜 ¥ 4/kg");
    ui->listWidget->addItem(pwatermelon);

    ui->listWidget->setViewMode(QListView::IconMode);

    connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem *)),this,SLOT(itemclickedFun(QListWidgetItem *)));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::itemclickedFun(QListWidgetItem *p)
{
    if(p->text() == "桃子 ¥ 10/kg")
    {
        static int nump = 0;
        nump ++;
        ui->lineEdit->setText("10");
        ui->lineEdit_2->setText(QString("%1").arg(nump));
        ui->lineEdit_3->setText(QString("%1").arg(nump*10));
        ui->label_4->setText("你选择的水果是桃子");
    }

    if(p->text() == "芒果 ¥ 20/kg")
    {
        static int numm = 0;
        numm ++;
        ui->lineEdit->setText("20");
        ui->lineEdit_2->setText(QString("%1").arg(numm));
        ui->lineEdit_3->setText(QString("%1").arg(numm*20));
        ui->label_4->setText("你选择的水果是芒果");
    }

    if(p->text() == "苹果 ¥ 15/kg")
    {
        static int numa = 0;
        numa ++;
        ui->lineEdit->setText("15");
        ui->lineEdit_2->setText(QString("%1").arg(numa));
        ui->lineEdit_3->setText(QString("%1").arg(numa*15));
        ui->label_4->setText("你选择的水果是苹果");
    }

    if(p->text() == "菠萝 ¥ 18/kg")
    {
        static int numpa = 0;
        numpa ++;
        ui->lineEdit->setText("18");
        ui->lineEdit_2->setText(QString("%1").arg(numpa));
        ui->lineEdit_3->setText(QString("%1").arg(numpa*18));
        ui->label_4->setText("你选择的水果是菠萝");
    }

    if(p->text() == "西瓜 ¥ 4/kg")
    {
        static int numw = 0;
        numw ++;
        ui->lineEdit->setText("18");
        ui->lineEdit_2->setText(QString("%1").arg(numw));
        ui->lineEdit_3->setText(QString("%1").arg(numw*4));
        ui->label_4->setText("你选择的水果是西瓜");
    }

    if(p->text() == "香蕉 ¥ 8/kg")
    {
        static int numb = 0;
        numb ++;
        ui->lineEdit->setText("8");
        ui->lineEdit_2->setText(QString("%1").arg(numb));
        ui->lineEdit_3->setText(QString("%1").arg(numb*4));
        ui->label_4->setText("你选择的水果是香蕉");
    }
}

文章来源:https://blog.csdn.net/smallwhitechen/article/details/135560820
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。