[Qt-interest] qobject_cast<>() and objects from dynamically loaded libraries

Greg Beauchesne greg_b at vision-play.com
Wed Jan 21 21:27:46 CET 2009


Greg Beauchesne wrote:

> Have you used Q_DECLARE_INTERFACE() for your classes? It should be put
> into the header file along with your class. Because your classes are
> defined in different dynamic libraries, they may not have the same
> QMetaObject for your Base class, even if QMetaObject::className() is the
> same for both of them, and without Q_DECLARE_INTERFACE(), two
> QMetaObjects that are essentially the same but located in different
> modules will not recognize each other.

Whoops; nevermind. My mistake. That's not how Q_DECLARE_INTERFACE() is
supposed to be used (although it looks like it'll work anyway if the
interface name is the same as the class name). Guess I should have
looked at my own code a little more closely.

In any case, the cause is the most likely same -- Base::staticMetaObject
in your main module is not the same object as Base::staticMetaObject in
your library; therefore the cast fails. Exporting the class as others
have already described in this thread is probably the better solution
for direct QObject descendants.



More information about the Qt-interest-old mailing list