.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_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
图标可自选
代码现象文章来源:https://www.toymoban.com/news/detail-812428.html
文章来源地址https://www.toymoban.com/news/detail-812428.html
到了这里,关于QT 小组件 列表框以及微调框的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!