QT第3天

发布时间:2024年01月14日

?

如上图界面,需求如下:

1、根据名字添加水果,并设置好单价

2、切换文件查看模式

3、点击任意水果可以显示单价

4、重量改变时,总价自动显示

//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 pbtnChangeModeSlotFunc();
    void listWidgetItemSlotFunc(QListWidgetItem *);
    void spinWeightChangeSlotFunc(double);

    void pbtnAddClickedSlotFunc();
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QListWidgetItem>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //设置水果对象

    QListWidgetItem *p1=new QListWidgetItem;
    p1->setText("奇异果");
    QIcon Icon1(":/icon/icon/奇异果.png");
    p1->setIcon(Icon1);
    p1->setData(Qt::UserRole,10.3);//设置单价
    ui->listWidget->addItem(p1);

    QListWidgetItem *p2=new QListWidgetItem;
    p2->setText("山竹");
    QIcon Icon2(":/icon/icon/山竹.png");
    p2->setIcon(Icon2);
    p2->setData(Qt::UserRole,15.6);//设置单价
    ui->listWidget->addItem(p2);

    QListWidgetItem *p3=new QListWidgetItem;
    p3->setText("杨桃");
    QIcon Icon3(":/icon/icon/杨桃.png");
    p3->setIcon(Icon3);
    p3->setData(Qt::UserRole,10.3);//设置单价
    ui->listWidget->addItem(p3);

    QListWidgetItem *p4=new QListWidgetItem;
    p4->setText("柠檬");
    QIcon Icon4(":/icon/icon/柠檬.png");
    p4->setIcon(Icon4);
    p4->setData(Qt::UserRole,9.5);//设置单价
    ui->listWidget->addItem(p4);

    QListWidgetItem *p5=new QListWidgetItem;
    p5->setText("桃子");
    QIcon Icon5(":/icon/icon/桃子.png");
    p5->setIcon(Icon5);
    p5->setData(Qt::UserRole,5.78);//设置单价
    ui->listWidget->addItem(p5);

    QListWidgetItem *p6=new QListWidgetItem;
    p6->setText("梨子");
    QIcon Icon6(":/icon/icon/梨子.png");
    p6->setIcon(Icon6);
    p6->setData(Qt::UserRole,4.91);//设置单价
    ui->listWidget->addItem(p6);

    QListWidgetItem *p7=new QListWidgetItem;
    p7->setText("椰子");
    QIcon Icon7(":/icon/icon/椰子.png");
    p7->setIcon(Icon7);
    p7->setData(Qt::UserRole,10.3);//设置单价
    ui->listWidget->addItem(p7);

    QListWidgetItem *p8=new QListWidgetItem;
    p8->setText("火龙果");
    QIcon Icon8(":/icon/icon/火龙果.png");
    p8->setIcon(Icon8);
    p8->setData(Qt::UserRole,9.99);//设置单价
    ui->listWidget->addItem(p8);

    QListWidgetItem *p9=new QListWidgetItem;
    p9->setText("榴莲");
    QIcon Icon9(":/icon/icon/榴莲.png");
    p9->setIcon(Icon9);
    p9->setData(Qt::UserRole,21.36);//设置单价
    ui->listWidget->addItem(p9);

    ui->spinWeight->setMinimum(0); //设置重量最小值

    connect(ui->pbtnChangeMode,SIGNAL(clicked()),this,SLOT(pbtnChangeModeSlotFunc()));
    connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem *)),this,SLOT(listWidgetItemSlotFunc(QListWidgetItem *)));
    connect(ui->spinWeight,SIGNAL(valueChanged(double)),this,SLOT(spinWeightChangeSlotFunc(double)));

    connect(ui->pbtnAdd,SIGNAL(clicked()),this,SLOT(pbtnAddClickedSlotFunc()));
}

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

//切换模式
void Widget::pbtnChangeModeSlotFunc()
{
    static int count=0;
    count++;
    (count%=2)==1?ui->listWidget->setViewMode(QListView::IconMode):ui->listWidget->setViewMode(QListView::ListMode);
}

//点击选项
void Widget::listWidgetItemSlotFunc(QListWidgetItem *)
{
    int index=ui->listWidget->currentRow(); //获取点击的index
    QListWidgetItem* p=ui->listWidget->item(index); //获取点击的对象
    double price=p->data(Qt::UserRole).toDouble();  //获取对象的自定义属性

    ui->spinPrice ->setValue(price);


}

//重量改变
void Widget::spinWeightChangeSlotFunc(double)
{
    double price=ui->spinPrice->value();
    double weight=ui->spinWeight->value();
    ui->spinTotal->setValue(price*weight);
}

//点击添加
void Widget::pbtnAddClickedSlotFunc()
{
    QString name=ui->lineEditName->text();
    double price=ui->spinPriceInit->value();

    QListWidgetItem *p=new QListWidgetItem;
    p->setText(name);
    QString IconName=QString(":/icon/icon/%1.png").arg(name);
    QIcon Icon3(IconName);
    p->setIcon(Icon3);

    p->setData(Qt::UserRole,price);//设置单价
    ui->listWidget->addItem(p);

    ui->lineEditName->setText("");
    ui->spinPriceInit->setValue(0);
}

?

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