[Qt-interest] qobject_cast<>() and objects from dynamically loaded libraries
Greg Beauchesne
greg_b at vision-play.com
Wed Jan 21 20:31:08 CET 2009
Dmitry Nezhevenko wrote:
>
> After loading library (LoadLibrary && GetProcAddress) and calling to
> getWidget() I'm trying to cast returned pointer to Base:
>
> Base *b = qobject_cast<Base *>(w);
>
> And b = 0 here. I'm sure that w is correct pointer. For example I can do:
>
> QString className = w->metaObject()->className();
>
> And className became "SomeImpl". Also debugger shows that w is instance of SomeImpl
> and contains Base.
>
> Also dynamic_cast works as expected. Any ideas why qobject_cast returns
> zero?
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.
More information about the Qt-interest-old
mailing list