在Qt的组合框中添加QObject

发布于 2024-08-08 18:25:36 字数 176 浏览 6 评论 0原文

我创建了一个自定义类,例如 MyClass。现在如何添加对 MyClass 引用的引用作为下面组合框中的第二个参数:

this->ui->comboBox->addItem("item-1", );

目的是当项目更改甚至被触发时,我想获取 MyClass 的特定类实例并进行相应的处理。

I have a custom class I created, say MyClass. Now how to add a reference to MyClass's reference as second parameter in the combo box below:

this->ui->comboBox->addItem("item-1", );

Purpose is to when item changed even is fired, i want to get that specific class instance of MyClass and process accordingly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灰色世界里的红玫瑰 2024-08-15 18:25:36

首先,您需要使用 Q_DECLARE_METATYPE(MyClass*),以便该类型可以在QVariant中使用。然后你可以像这样添加该项目:

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

并将其取回:

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

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*>();
绳情 2024-08-15 18:25:36

上面的答案语法有点不正确,

在MyClass头文件中使用Q_DECLARE_METATYPE(MyClass*),这样该类型就可以在QVariant中使用。

像这样添加项目:

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

并把它找回来:
this->ui->combobox->itemData(x).value();

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();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文