QT 1.12

发布时间:2024年01月18日

.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->listWidget->setViewMode(QListView::IconMode);
    connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem *)),this,SLOT(listWidgetItemClickedSlotFun(QListWidgetItem *)   ));
    connect(ui->doubleSpinBox_2,SIGNAL(valueChanged(double)),this,SLOT(doublespinboxValueChangedSlotFun(double)));

    for(int i=0;i<8;i++){
        QListWidgetItem *p = new QListWidgetItem;
        p->setText(shuiguo[i]);
        QIcon icoC(picture[i]);
        p->setIcon(  QIcon( icoC )  );
        ui->listWidget->addItem(p);
    }

}

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

void Widget::doublespinboxValueChangedSlotFun(double)
{
    int index = 0;
    index = ui->listWidget->currentRow();
    ui->doubleSpinBox->setValue(jiage[index]);
    double zl = ui->doubleSpinBox_2->value();
    double value = jiage[index]*zl;
    ui->doubleSpinBox_3->setValue(value);
}

void Widget::listWidgetItemClickedSlotFun(QListWidgetItem *)
{
    int index = 0;
    index = ui->listWidget->currentRow();
    ui->doubleSpinBox->setValue(jiage[index]);
    double zl = ui->doubleSpinBox_2->value();
    double value = jiage[index]*zl;
    ui->doubleSpinBox_3->setValue(value);
}

.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();
    QString shuiguo[8]={"苹果","香蕉","葡萄","橘子","梨","菠萝","草莓","西瓜"};
    QString picture[8]={":/ico/ico/Apple.ico",":/ico/ico/Banana.ico",\
                    ":/ico/ico/Grape.ico",":/ico/ico/Orange.ico",\
                    ":/ico/ico/Papaya.ico",":/ico/ico/Pineapple.ico",\
                    ":/ico/ico/Strawberry.ico",":/ico/ico/Watermelon.ico"};
    double jiage[8]={4.18,3,4.5,8.8,3.4,2.4,23.3,2.2};

public slots:
    void doublespinboxValueChangedSlotFun(double);
    void listWidgetItemClickedSlotFun(QListWidgetItem *);

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

运行

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