QVariant
1.void setValue(const T &value)
2.T Value()
3.QString s = v.value<QString>();
4.Q_DECLARE_METATYPE(text)
Widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
struct MyType
{
int x = 0;
int y = 0;
};
Q_DECLARE_METATYPE(MyType);
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QVariant var;
MyType my;
my.x = 10;
var.setValue(my);
ui->comboBox->clear();
ui->comboBox->addItem("Item001",var);
ui->comboBox->addItem("Item002");
//图标
QIcon icon("./Resources/test.png");
ui->comboBox->addItem(icon,"Item003");
}
void Widget::Click()
{
qDebug()<<ui->comboBox->itemText(0);
QVariant var = ui->comboBox->itemData(0);
qDebug()<<"var.value<MyType>().x = "<<var.value<MyType>().x;
}
Widget::~Widget()
{
delete ui;
}
xz@xiaqiu:~/study/csdn/day0/QComboxBox/build$ ./testqt
"Item001"
var.value<MyType>().x = 10
"Item001"
var.value<MyType>().x = 10
"Item001"
var.value<MyType>().x = 10
xz@xiaqiu:~/study/csdn/day0/QComboxBox/build$