QT 小组件 列表框以及微调框

QT 小组件 列表框以及微调框_第1张图片

.cpp文件

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QListWidgetItem *pPhone = new QListWidgetItem;
    pPhone->setText("西瓜");
    pPhone->setIcon(QIcon(":/ls/iPhone 512x512.ico"));
    ui->listWidget->addItem(pPhone);

    QListWidgetItem *papple = new QListWidgetItem;
    papple->setText("苹果");
    papple->setIcon(QIcon(":/ls/Notes 512x512.ico"));
    ui->listWidget->addItem(papple);

    QListWidgetItem *pbanana = new QListWidgetItem;
    pbanana->setText("香蕉");
    pbanana->setIcon(QIcon(":/ls/Stocks 512x512.ico"));
    ui->listWidget->addItem(pbanana);

    QListWidgetItem *porange = new QListWidgetItem;
    porange->setText("橘子");
    porange->setIcon(QIcon(":/ls/Weather 512x512.ico"));
    ui->listWidget->addItem(porange);

    //模式切换
    ui->listWidget->setViewMode(QListView::IconMode);

    //信号
    connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem *)),this,SLOT(listWidgetItemClickedSlotFun(QListWidgetItem *)));

    //变化重量算价格
    connect(ui->weigthSpinBox,SIGNAL(valueChanged(double)),this,SLOT(weigthSpinBoxChangedSlotFun(double)));

    //设置前缀

    ui->valueSpinBox->setPrefix(" $ ");
    ui->weigthSpinBox->setSuffix(" kg ");



}

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

void Widget::listWidgetItemClickedSlotFun(QListWidgetItem * p)
{
    QString txt = p->text();
    if(txt == "西瓜")
     {
        //设置价格
        ui->valueSpinBox->setValue(1.75);
        ui->sumSpinBox->setValue(ui->valueSpinBox->value());
     }
    else if(txt == "苹果")
     {
        ui->valueSpinBox->setValue(4.2);
        ui->sumSpinBox->setValue(ui->valueSpinBox->value());
    }
    else if(txt == "香蕉")
     {
        ui->valueSpinBox->setValue(3.5);
        ui->sumSpinBox->setValue(ui->valueSpinBox->value());

    }
    else if(txt == "橘子")
     {
        ui->valueSpinBox->setValue(2.5);
        ui->sumSpinBox->setValue(ui->valueSpinBox->value());
    }
}

void Widget::weigthSpinBoxChangedSlotFun(double)
{
    double weigth = ui->weigthSpinBox->value();
    double value  = ui->valueSpinBox->value();
    ui->sumSpinBox->setValue(weigth*value);
}

.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
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 listWidgetItemClickedSlotFun(QListWidgetItem *);
    void weigthSpinBoxChangedSlotFun(double);
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

图标可自选 

代码现象

QT 小组件 列表框以及微调框_第2张图片

你可能感兴趣的:(qt,命令模式,开发语言)