[Qt-interest] How to discover if a custom type stored into a QVariant inherits the QObject class

Stephen Kelly steveire at gmail.com
Fri Mar 19 15:41:05 CET 2010


Flavio Castelli wrote:

> Is it possible to know at run-time if a custom type stored into a QVariant
> instance inherits the QObject class?
> 
> In the following example Person is a child of QObject:
> 
>   Person* p = new Person();
> 
>   QVariant v;
>   v.setValue(p);
>   qDebug() << v.canConvert<QObject*>(); // returns false
>   qDebug() << v.typeName(); // return Person*
>   qDebug() << QVariant::nameToType(v.typeName()); //return
>   QVariant::UserType delete p;
> 
> Thanks in advance
> Flavio


MyObject *obj = new MyObject;
QVariant v = QVariant::fromValue(static_cast<QObject*>( obj ) );

...

QObject *o = v.value<QObject*>();
qDebug() << o->inherits("MyObject");






More information about the Qt-interest-old mailing list