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

Andreas Pakulat apaku at gmx.de
Fri Mar 19 11:47:04 CET 2010


On 19.03.10 10:35:06, Flavio Castelli wrote:
> 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
> }

You can't do that, QVariant merely stores a void* internally, so you need
to tell it how to get something useful out of that again. Hence you have to
know what was originally put into the variant. Not sure wether dynamic_cast
works that way, but you could ask the QVariant for a void* and then try to
use dynamic_cast<QObject*> to determine wether its a QObject or not. i.e.
do the check yourself instead of letting QVariant do it.

Andreas

-- 
You will get what you deserve.



More information about the Qt-interest-old mailing list