[Interest] qobject_cast

Ch'Gans chgans at gna.org
Thu Apr 20 09:17:20 CEST 2017


On 20 April 2017 at 19:04, Igor Mironchik <igor.mironchik at gmail.com> wrote:
> Hi,
>
> If sender() is a Channel*, then qobject_cast should not fail.
>
> Try to add this to your slot, this will print the inheritance list
>
>     qDebug() << "Looking for" << &Channel::staticMetaObject <<
> Channel::staticMetaObject.className() << "on" << sender() <<
> qobject_cast< Channel* > ( sender() )
>     const QMetaObject *mo = sender->metaObject();
>    while (mo != nullptr) {
>         qDebug() << mo << mo->className() << (mo ==
> &Channel::staticMetaObject ? "<-" : "");
>         mo = mo->superClass();
>     }
>
> This is roughly how QMetaObject::inherits() works. if you get the
> "<-", then qobject_cast should work as expected.
>
>
> I got:
>
> Looking for 0xe6b188 Globe::Channel on Globe::ComoChannel(0x4500270, name =
> "Channel") QObject(0x0)
>
> 0xd49548 Globe::ComoChannel
>
> 0xd4f060 Globe::Channel
>
> 0x6683c44c QObject
>
>
> And as I can guess it's because of static linking. I.e. I have static lib
> where Channel defined. And this lib linking to app and to plugin.

Sounds like a bad idea indeed! You have two copies of the same meta-data....
Use dynamic libraries instead.

Chris

>
>
> Am I right that I should use dynamic libs?
>
>



More information about the Interest mailing list