[Qt-interest] Meta Object for Class Name?
Sean Harmer
sean.harmer at maps-technology.com
Wed Nov 11 20:03:17 CET 2009
Josiah Bryan wrote:
> Sean Harmer wrote:
>
>> Josiah Bryan wrote:
>>
>>> Is there a way to get the meta object for a class given the class name
>>> (assuming its a QObject, of course.)
>>>
>>> Ideally, I'd like to be able to construct a class just from its name, say:
>>>
>>> FooBar *foo = (FooBar*) QMetaObject::defaultConstructor("FooBar");
>>>
>>> Is that pie-in-the-sky or is that somehow possible?
>>>
>>>
>> I think you are after QObject* QMetaObject::newInstance():
>>
>> http://doc.trolltech.com/4.6-snapshot/qmetaobject.html#newInstance
>>
>>
>
> Awesome! Thanks, Sean. Maybe I'm blind, but I didn't see anything in the
> docs where I can get the QMetaObject for a class name, e.g. something like:
>
> static QMetaObject * QMetaObject::metaObject(const char * className);
>
> Any ideas if thats possible or not?
>
I am not aware of any way of doing this directly. Take a look in the
Q_OBJECT macro maybe something gets written in there that allows this?
One possibility would be to use the QObject::metaObject() or
QObject::staticMetaObject() functions depending on if you already have a
pointer to an object of yoru QObject class or not.
You could always insert pointers to yoru meta objects into a global
QHash<QString,QMetaObject*> container and do the lookup using that. Just
an idea. Others may have a better method(s).
It would be nice to have a static QMetaObject* QMetaObject::metaObject(
const char* ) method though.
Cheers,
Sean
> Thanks!
> -josiah
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
More information about the Qt-interest-old
mailing list