[Qt-interest] Is it possible to discover if a custom type stored into a QVariant inherits the QObject class?

Flavio Castelli flavio at castelli.name
Fri Mar 19 10:35:06 CET 2010


On Thursday 18 March 2010 20:59:28 K. Frank wrote:
> Yes, you can use the standard run-time type information facility of C++
> (assuming you haven't turned it off somehow).
> get pointer-to-Person out of the QVariant:
> 
>    Person *pp = v.value<Person *>();
> 
> see if it's a pointer-to-QObject:
> 
>    QObject *pq = dynamic_cast<QObject *>(pp);
> 
> dynamic_cast returns null if *pp doesn't inherit QObject:

I cannot do that because I'm analyzing a generic QVariant object. I'm my real 
world code I don't have even included the "person.h" header.

This is what I would like  to do:
//foo is a QVariant object
if (foo.type == QVariant::QVariantUserType) {
  // check if foo contains a QObject child and then do something
}

Cheers
Flavio

Ps: sorry I had to change the title of the thread to make clear I'm looking 
for a solution



More information about the Qt-interest-old mailing list