QT day3


#include "widget.h"
#include "ui_widget.h"
#include 
#include 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    static int sum=0;//计数
    //设置显示方式
    ui->listWidget->setViewMode(QListView::IconMode);
 
    QListWidgetItem *pCamera = new QListWidgetItem;
    pCamera->setText("苹果");
    QIcon icoC (":/tp/9042135_apple_icon.ico");
    pCamera->setIcon( icoC );
    ui->listWidget->addItem(pCamera);
 
    QListWidgetItem *pMpas = new QListWidgetItem;
    pMpas->setText("香蕉");
    pMpas->setIcon(  QIcon(":/tp/8665003_carrot_food_icon.ico" ) );
    ui->listWidget->addItem( pMpas );
 
    QListWidgetItem *pw = new QListWidgetItem;
    pw->setText("西瓜");
    pw->setIcon(  QIcon(":/tp/9042904_pizza_slice_icon.ico" ) );
    ui->listWidget->addItem( pw );
    //重置按钮绑定
    connect(ui->btnreset,SIGNAL(clicked()),this,SLOT(btnResetClickedSlotFun()));
    //列表单击与priceIineEdit
     connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem *)), this,SLOT( listWidgetItemClickedSlotFun(QListWidgetItem *)));
}
 
Widget::~Widget()
{
    delete ui;
}
void Widget::listWidgetItemClickedSlotFun(QListWidgetItem *p)
{
 
     QString txt = p->text();
            if(txt =="苹果"){
               ui->priceIineEdit->setText("5元");
               ui->IineEdit->setText("1.66Kg");
               sum=5*1.66;
               QString str = QString("%1元").arg(this->sum);
               ui->sumIineEdit->setText(str);
 
            }
            else if(txt=="香蕉"){
                ui->priceIineEdit->setText("4元");
                ui->IineEdit->setText("1.5Kg");
                sum=sum+4*1.5;
                QString str = QString("%1元").arg(this->sum);
                ui->sumIineEdit->setText(str);
 
            }
            else{
                if(txt=="西瓜"){
                    ui->priceIineEdit->setText("6元");
                    ui->IineEdit->setText("4.5Kg");
                    sum=sum+6*4.5;
                    QString str = QString("%1元").arg(this->sum);
                    ui->sumIineEdit->setText(str);
                    }
                }
}
 
void Widget::btnResetClickedSlotFun()
{
    sum = 0;
    QString str = QString("%1元").arg(this->sum);
    ui->sumIineEdit->setText(str);
    ui->priceIineEdit->setText("0");
    ui->IineEdit->setText("0KG");
}

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