如何将QVariant转换为自定义类? [英] How can I cast a QVariant to custom class?

查看:1473
本文介绍了如何将QVariant转换为自定义类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Momentics IDE(原生SDK)开发BlackBerry 10移动应用程序。

I'm developing a BlackBerry 10 mobile application using the Momentics IDE (native SDK).

我有一个listview,我想处理它的项目点击C + (我需要使用C ++不是QML)。

I have a listview which I want to handle its items click with C++ (I need to use C++ not QML).

我可以使用连接指令获得索引路径,但我有解析QVariant到自定义类;

I can get the index path using the "connect" instruction, but I have problem with parsing a QVariant to a custom class ;

Q_ASSERT(QObject::connect(list1, SIGNAL(triggered(QVariantList)), this, SLOT(openSheet(QVariantList))));

QVariant selectItem = m_categoriesListDataModel->data(indexPath);

我试着使用下面的静态类型

I tried to use the static cast like below

Category* custType = static_cast<Category*>(selectItem);

但返回:

"invalid static_cast from type 'QVariant' to type 'Category*'"

推荐答案

您可以尝试使用 qvariant_cast qobject_cast

QObject *object = qvariant_cast<QObject*>(selectItem);
Category *category = qobject_cast<Category*>(object);

此外,不要将任何持久化语句放入Q_ASSERT。当assert未启用时,将不会使用它。

Also, never put any persistent statement into Q_ASSERT. It will not be used when the assert is not enabled.

这篇关于如何将QVariant转换为自定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆