题目:实现简单水果的价格重量计算
点击一次水果重量+1 自动计算总价
文章来源:https://www.toymoban.com/news/detail-786329.html
代码:
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://www.toymoban.com/news/detail-786329.html
到了这里,关于QT周五作业的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!