How can I cast a QVariant to custom class?
Asked Answered
U

2

27

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 ?

Uphold answered 23/6, 2014 at 9:50 Comment(2)
can you add code to show how you add data to model ?Mortise
Don't use Q_ASSERT in such cases! Q_ASSERT will not work in release build, and signal will not be connected.Deshawndesi
S
30

You could try using qvariant_cast and qobject_cast.

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

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

Spinach answered 23/6, 2014 at 9:55 Comment(1)
thanks for the information. About "Q_DECLARE_METATYPE", I tried to put it in the class definition like this example but it doesn't work; it returns "within this context" (I think this is because the class is a QObject one "class Category: public QObject") and when I try to put it wherever outisde the class definition it returns "a template declaration cannot appear at block scope".Uphold
E
41

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);
Elide answered 23/6, 2014 at 10:14 Comment(4)
Thank you for your help. I tried to put the "Q_DECLARE_METATYPE" instruction like you describe but it returns an error "within this context", I think thats because my custom type inherits from the QOBject class : "class Category: public QObject"Uphold
@FinalContest is right. Question does not mention your were treating a QObject derived type.Elide
Your comment helped me out a lot, I feel like yours should be the answer, and that ldapps should be an answer to an entirely different question, as J.M.J didn't specify they were deriving from QObject in the original postAndee
There is a typo, the right MACRO is Q_DECLARE_METATYPE (no underscore between META and TYPE) at least on Qt 15.8Newmown
S
30

You could try using qvariant_cast and qobject_cast.

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

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

Spinach answered 23/6, 2014 at 9:55 Comment(1)
thanks for the information. About "Q_DECLARE_METATYPE", I tried to put it in the class definition like this example but it doesn't work; it returns "within this context" (I think this is because the class is a QObject one "class Category: public QObject") and when I try to put it wherever outisde the class definition it returns "a template declaration cannot appear at block scope".Uphold

© 2022 - 2024 — McMap. All rights reserved.