Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

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

but it returns :

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

Can anyone help me on this ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
782 views
Welcome To Ask or Share your Answers For Others

1 Answer

EDIT: works for non QObject derived type (see Final Contest's answer for this case)

First of all, you need to register your type to be part of QVariant managed types

//customtype.h
class CustomType {
};

Q_DECLARE_METATYPE(CustomType)

Then you can retrieve your custom type from QVariant in this way :

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

which is equivalent to:

CustomType ct = qvariant_cast<CustomType>(myVariant);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...