本文主要是介绍QT 小组件 列表框以及微调框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
.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 <QWidget>
#include <QListWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();public slots:void listWidgetItemClickedSlotFun(QListWidgetItem *);void weigthSpinBoxChangedSlotFun(double);
private:Ui::Widget *ui;
};
#endif // WIDGET_H
图标可自选
代码现象
这篇关于QT 小组件 列表框以及微调框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!