Add QObject in the combo box of Qt

谁说我不能喝 提交于 2019-12-04 18:24:36

First you need to use Q_DECLARE_METATYPE(MyClass*), so that the type can be used in QVariant. Then you can add the item like this:

this->ui->comboBox->addItem("item-1", QVariant::fromValue(myClass));

And get it back:

this->ui->combobox->itemData(x).value<MyClass*>();

Above answer syntax is slightly incorrect,

use Q_DECLARE_METATYPE(MyClass*), in the MyClass header file, so that the type can be used in QVariant.

add the item like this:

this->ui->comboBox->addItem("item-1", QVariant::fromValue(myClass));

And get it back: this->ui->combobox->itemData(x).value();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!