[Qt-interest] QMetaObject::newInstance and plugins

Thiago Macieira thiago at kde.org
Thu Aug 5 05:55:37 CEST 2010


On Wednesday 4. August 2010 14.32.28 Stephen D'Angelo wrote:
> Hello all,
> 
> I have a plugin interface that returns a QMetaObject of a class that
> inherits from QObject (SomeClass).  With this QMetaObject, I call
> newInstance to create an instance of its QObject.  When I then call
> qobject_cast<SomeClass *> to cast this QObject to SomeClass, it always
> returns 0.
> 
> However, the following works fine from within the main program (i.e. not
> getting the QMetaObject from the plugin):
> qobject_cast<SomeClass*>(SomeClass::staticMetaObject.newInstance());
> 
> Could it be an issue with the fact that SomeClass is compiled into both the
> main program and the plugin?  Should it be pulled out to a separate
> library?

Yes and yes.

If the plugin returns a pointer to a class SomeClass, it means the plugin had 
access to that class. If the application can cast to it, it means the 
application has access to that class.

The only possibility that addresses both conditions is when SomeClass is in a 
library that both the application and the plugin link to.

This is a very common problem. People don't know that the most important part 
of a plugin mechanism is the library.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100805/cae11c7a/attachment.bin 


More information about the Qt-interest-old mailing list