方法
- 携带(存储)一个数据:
addItem(const QString &text, const QVariant &userData = QVariant())
- 携带(存储)多个数据:
setModel(QAbstractItemModel *model)
携带(存储)一个数据
// 添加数据
for(auto &var:infos)
{
ui->comboBox_only->addItem(var.name,var.idFA);
}
// 获取数据
int idFA =ui->comboBox_only->currentData().toInt();
QString name =ui->comboBox_only->currentText();
携带(存储)多个数据
// 添加数据
auto pModel = new QStandardItemModel(this);
for(auto &var:infos)
{
auto pItem = new QStandardItem(var.name);
pItem->setData(var.idFA,Qt::UserRole);
pItem->setData(var.equipId,Qt::UserRole+1);
pItem->setData(var.equipNum,Qt::UserRole+2);
pModel->appendRow(pItem);
}
qDebug()<<"pModel.rowCount() ="<<pModel->rowCount();
ui->comboBox->setModel(pModel);
// 获取数据
auto pModel=qobject_cast<QStandardItemModel *>(ui->comboBox->model());
if(pModel){
int idFA =pModel->data(pModel->index(index,0),Qt::UserRole).toInt();
QString name=ui->comboBox->currentText();
QString equipId=pModel->data(pModel->index(index,0),Qt::UserRole+1).toString();
int equipNum=pModel->data(pModel->index(index,0),Qt::UserRole+2).toInt();
}
全部内容
#include "uniteitemdata.h"
#include "ui_uniteitemdata.h"
#include <QStandardItemModel>
#include <QStandardItem>
#include <QVector>
#include <QDebug>
#pragma execution_character_set("utf-8")
namespace{
struct Test {
int idFA; //方案ID
QString name; //名称
QString equipId; //装备ID
int equipNum; //装备数量
};
QVector<Test> infos = {
{1, "奥迪A6L", "9001",101},
{2, "宝马X7", "9003",102},
{3, "迈巴赫S", "9002",103},
{4, "库里南", "9020",104},
{5, "丹拿","9002",105},
{6, "柏林之声", "9004",106},
{7, "奔驰", "9006",107},
{8, "林肯", "9008",108},
};
}
UniteItemData::UniteItemData(QWidget *parent)
: QWidget(parent)
, ui(new Ui::UniteItemData)
{
ui->setupUi(this);
setWindowFlag(Qt::WindowStaysOnTopHint);
ui->comboBox->clear();
// 携带(存储)多个数据
auto pModel = new QStandardItemModel(this);
for(auto &var:infos)
{
auto pItem = new QStandardItem(var.name);
pItem->setData(var.idFA,Qt::UserRole);
pItem->setData(var.equipId,Qt::UserRole+1);
pItem->setData(var.equipNum,Qt::UserRole+2);
pModel->appendRow(pItem);
}
qDebug()<<"pModel.rowCount() ="<<pModel->rowCount();
ui->comboBox->setModel(pModel);
// 携带(存储)一个数据
for(auto &var:infos)
{
ui->comboBox_only->addItem(var.name,var.idFA);
}
ui->textBrowser->clear();
}
UniteItemData::~UniteItemData()
{
delete ui;
}
void UniteItemData::on_comboBox_currentIndexChanged(int index)
{
auto pModel=qobject_cast<QStandardItemModel *>(ui->comboBox->model());
if(pModel){
int idFA =pModel->data(pModel->index(index,0),Qt::UserRole).toInt();
QString name=ui->comboBox->currentText();
QString equipId=pModel->data(pModel->index(index,0),Qt::UserRole+1).toString();
int equipNum=pModel->data(pModel->index(index,0),Qt::UserRole+2).toInt();
ui->textBrowser->clear();
QString str = "-";// 重复30次
ui->textBrowser->append(str.repeated(15)+"携带(存储)多个数据"+str.repeated(15));
ui->textBrowser->append(QString("方案ID =")+QString::number(idFA));
ui->textBrowser->append(QString("名称 =")+QString(name));
ui->textBrowser->append(QString("装备ID =")+QString(equipId));
ui->textBrowser->append(QString("装备数量=")+QString::number(equipNum));
}
}
void UniteItemData::on_comboBox_only_currentIndexChanged(int index)
{
ui->textBrowser->clear();
QString str = "-";// 重复30次
ui->textBrowser->append(str.repeated(15)+"携带(存储)一个数据"+str.repeated(15));
int idFA =ui->comboBox_only->currentData().toInt();
QString name =ui->comboBox_only->currentText();
ui->textBrowser->append(QString("方案ID =")+QString::number(idFA));
ui->textBrowser->append(QString("名称 =")+QString(name));
}
效果