18

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

我有一个列表视图,我想用 C++ 处理它的项目(我需要使用 C++ 而不是 QML)。

我可以使用“connect”指令获取索引路径,但是在将 QVariant 解析为自定义类时遇到问题;

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

QVariant selectItem = m_categoriesListDataModel->data(indexPath);

我尝试使用下面的静态演员表

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

但它返回:

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

谁可以帮我这个事 ?

4

2 回答 2

26

编辑:适用于非 QObject 派生类型(请参阅本案例的最终竞赛答案)

首先,您需要将您的类型注册为 QVariant 托管类型的一部分

//customtype.h
class CustomType {
};

Q_DECLARE_METATYPE(CustomType)

然后您可以通过以下方式检索您的自定义类型QVariant

CustomType ct = myVariant.value<CustomType>();

这相当于:

CustomType ct = qvariant_cast<CustomType>(myVariant);
于 2014-06-23T10:14:29.027 回答
23

您可以尝试使用qvariant_castqobject_cast

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

此外,永远不要将任何持久性语句放入 Q_ASSERT。未启用断言时将不会使用它。

于 2014-06-23T09:55:47.610 回答