?
?
功能函数
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//添加元素
ui->comboBoxFruit->addItem("please chose fruit");
ui->comboBoxFruit->addItem("apple");
ui->comboBoxFruit->addItem("banana");
ui->comboBoxFruit->addItem("pear");
//设置前缀后缀
ui->dSpPrice->setPrefix("$");
ui->dSpTotal->setPrefix("$");
ui->spNumber->setSuffix("KG");
//设置默认值
ui->spNumber->setValue(5);
ui->dSpPrice->setValue(5.55);
ui->dSpTotal->setValue(27.75);
connect(ui->spNumber,SIGNAL(valueChanged(int)),this,SLOT(spNumberValueChangedSlotFun(int) ));
connect(ui->dSpPrice,SIGNAL(valueChanged(double)),this,SLOT(dSpPricevalueChanged(double)));
//选中更新价格,自动计算总价
connect(ui->comboBoxFruit,SIGNAL(currentIndexChanged(const QString)),this,SLOT(comboBoxFruitCurrentIndexChangedSlotFun(const QString) ));
//重置
connect(ui->btnReset,SIGNAL(clicked()),this,SLOT(btnResetSlotFun() ));
}
Widget::~Widget()
{
delete ui;
}
void Widget::comboBoxFruitCurrentIndexChangedSlotFun(const QString str)
{
QString str1 = QString("选中了%1").arg(str);
ui->btnchooseShow->setText(str1);
if(ui->comboBoxFruit->currentText()=="apple")
{
ui->spNumber->setValue(2);
ui->dSpPrice->setValue(3.8);
ui->dSpTotal->setValue(ui->spNumber->value()*ui->dSpPrice->value());
}
if(ui->comboBoxFruit->currentText()=="pear")
{
ui->spNumber->setValue(6);
ui->dSpPrice->setValue(7.8);
ui->dSpTotal->setValue(ui->spNumber->value()*ui->dSpPrice->value());
}
if(ui->comboBoxFruit->currentText()=="pear")
{
ui->spNumber->setValue(3);
ui->dSpPrice->setValue(5.3);
ui->dSpTotal->setValue(ui->spNumber->value()*ui->dSpPrice->value());
}
}
void Widget::spNumberValueChangedSlotFun(int v)
{
int num = ui->spNumber->value();
double price = ui->dSpPrice->value();
ui->dSpTotal->setValue(num*price);
}
void Widget::dSpPricevalueChanged(double v)
{
int num = ui->spNumber->value();
double price = ui->dSpPrice->value();
ui->dSpTotal->setValue(num*price);
}
void Widget::btnResetSlotFun()
{
ui->spNumber->setValue(0);
ui->dSpPrice->setValue(0.00);
ui->dSpTotal->setValue(0.00);
}
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
public slots:
void comboBoxFruitCurrentIndexChangedSlotFun(const QString);
void spNumberValueChangedSlotFun(int);
void dSpPricevalueChanged(double);
void btnResetSlotFun();
};
#endif // WIDGET_H