QT中如何在ComboBox中设置存放自定义类型
2017-04-28 10:23
323 查看
假如我有一个自定义的类Scene,如今需要把Scene*的指针地址放入到ComboBox中。
1、首先在Dlg文件的顶端声明自定义类型,使QVariant::fromValue()方法可以识别自定义类型。
Q_DECLARE_METATYPE(Scene *)
2、在ComboBox元素插入的时候,把指针地址放入的QVariant中
ui.comboBox->insertItem(0, tr("All"));
std::map<Scene*, std::set<ResourceFile*, FileCompare>> sceneMap = materialData_->sceneMap();
std::map<Scene*, std::set<ResourceFile*, FileCompare>>::iterator itor;
int i = 1;
for (itor = sceneMap.begin(); itor != sceneMap.end(); itor++, i++)
{
ui.comboBox->insertItem(i, stdStringToQString(itor->first->name()), QVariant::fromValue(itor->first));
}
3、在响应ComboBox的currentIndexChanged(int)信号槽时
Scene* p = (Scene*)(ui.comboBox->itemData(index).value<Scene *>());
index为信号槽传入的当前ComboBox的索引。
1、首先在Dlg文件的顶端声明自定义类型,使QVariant::fromValue()方法可以识别自定义类型。
Q_DECLARE_METATYPE(Scene *)
2、在ComboBox元素插入的时候,把指针地址放入的QVariant中
ui.comboBox->insertItem(0, tr("All"));
std::map<Scene*, std::set<ResourceFile*, FileCompare>> sceneMap = materialData_->sceneMap();
std::map<Scene*, std::set<ResourceFile*, FileCompare>>::iterator itor;
int i = 1;
for (itor = sceneMap.begin(); itor != sceneMap.end(); itor++, i++)
{
ui.comboBox->insertItem(i, stdStringToQString(itor->first->name()), QVariant::fromValue(itor->first));
}
3、在响应ComboBox的currentIndexChanged(int)信号槽时
Scene* p = (Scene*)(ui.comboBox->itemData(index).value<Scene *>());
index为信号槽传入的当前ComboBox的索引。
相关文章推荐
- Qt Q_DECLARE_METATYPE说明——让自定义的类型设置到QVariant
- 如何在Qt中使用自定义数据类型
- Flex中如何通过设置fontFamily样式在ComboBox控件中使用自定义嵌入字体的例子
- QT 4.7 DBUS Binding如何支持自定义数据类型的传输(一)
- TreeSet集合存放自定义类型的对象,可以使用比较器,实现Comparable接口自己设置排序方式
- 如何在Qt中使用自定义数据类型 (zz)
- 如何在Qt中使用自定义数据类型
- 如何在Qt中使用自定义数据类型
- 如何在Qt中使用自定义数据类型
- Qt 中如何实现对图片设置透明色显示 .
- Qt如何设置窗口的背景图片?
- 创建自定义Qt类型
- android中给Dialog设置的动画如何自定义修改参数
- QT自定义部件在VS2008中moc预编译手动设置备忘
- WordPress中设置Post Type自定义文章类型的实例教程
- Qt之QComboBox(基本应用、代理设置)
- 如何去掉IE7启动时的"自定义您的设置"
- WinForm如何设置comboBox的高度而不改变字体
- Android 自定义view-如何设置TextView drawableLeft 图片大小?
- 【Android】如何设置一个自定义的Button