[Qt-interest] Retrieving QMetaObject from a Qt Meta Type ID

Sean Harmer sean.harmer at maps-technology.com
Mon Jan 24 19:58:02 CET 2011


Hi,

On Monday 24 January 2011 17:04:56 Gustavo de Sá Carvalho Honorato wrote:
> I have a method that returns QVariant with any type. I have some
> pointer types pointing to an object derived from QObject, which are
> registered using Q_DECLARE_METATYPE. When this method returns these
> pointers to QObject derived objects, I need to list all properties,
> slots and others thinks related to its meta object. The problem is
> that I didn't find any way disconver if this QVariant is holding a
> pointer to a QObject derived type to get the QMetaObject and finally
> list the properties. The purpouse of this is to expose QObject to a
> scripting system, like QScriptEngine does.
> 
> Any ideas?

There are probably more elegant ways but this should work:

int typeId = myVariant.userType();
QObject* object = QMetaType::construct( typeId );
QMetaObject metaObject = object->metaObject();
...
QMetaType::destroy( typeId, object );

Or if you want to use your existing object stored in the variant use 
qobject_cast to get it back to a QObject pointer and then get the metaobject 
from that.

HTH,

Sean



More information about the Qt-interest-old mailing list