On 20 April 2017 at 18:10, Igor Mironchik <igor.mironc...@gmail.com> wrote: > > > 20.04.2017 9:01, Ch'Gans пишет: > > On 20 April 2017 at 17:56, Igor Mironchik <igor.mironc...@gmail.com> wrote: > > 20.04.2017 8:47, Ch'Gans пишет: > > On 20 April 2017 at 17:03, Igor Mironchik <igor.mironc...@gmail.com> > wrote: > > Hi, > > > 20.04.2017 7:52, Ch'Gans пишет: > > On 20 April 2017 at 16:38, Igor Mironchik <igor.mironc...@gmail.com> > wrote: > > Hi, > > What the benefits of qobject_cast<> on simple static_cast<>? Or there > is > no > > qobject_cast > - works only on QObject (obviously) > - returns nullptr if the object cannot be casted to the required type > > And it can fail if QObject derived pointer was returned from plugin. > > Is this a statement or a question? > > if I'm not wrong, qobject_cast relies on QMetaObject > (object->metaObject() and Class::staticMetaObject()). > > Actually, i've just checked the sources and qobject_cast<T*>(object) > returns "static_cast<T*>(T::staticMetaObject().cast(object))" and > QMetaObject::cast(obj) returns "(obj && > obj->metaObject()->inherits(this)) ? obj : nullptr" > > QMetaObject allows you to walk the class inheritance hierarchy, and > inherits() does exactly this. > I don't think that the fact that the object comes from a plugin can > make inherits() fail. > If you are able to load the plugin successfully, it means that you > have no unresolved symbols. > > Exactly! qobject_cast<> will fail. > > Look in application you have: > > class Channel > : public QObject > {}; > > In plugin you have: > > class MyChannel > : public Channel > {}; > > Channel * create(); > > In application you call: > > Channel * ch = create(); > > And use this object in your application. > > Then in one slot in application you can have: > > void mySlot() > { > Channel * ch = qobject_cast< Channel* > ( sender() ); > // You are 100% sure that sender() is Channel!!! > // And this cast will fail!!! > > Try to call dumpObjectInfo(sender()); > > > OBJECT Globe::ComoChannel::Channel
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. Chris > > SIGNALS OUT > > signal: destroyed(QObject*) > > signal: destroyed() > > signal: objectNameChanged(QString) > > signal: sourceUpdated(Como::Source) > > <functor or function pointer> > > signal: sourceDeregistered(Como::Source) > > <functor or function pointer> > > signal: connected() > > <functor or function pointer> > > <functor or function pointer> > > signal: disconnected() > > <functor or function pointer> > > <functor or function pointer> > > <functor or function pointer> > > signal: messagesRate(int) > > <functor or function pointer> > > signal: aboutToConnectToHost(QHostAddress,ushort) > > <functor or function pointer> > > signal: aboutToDisconnectFromHost() > > <functor or function pointer> > > signal: aboutToSendGetListOfSources() > > <functor or function pointer> > > SIGNALS IN > > <-- QTimer::unnamed <unknown> > > <-- QTimer::unnamed <unknown> > > <-- Como::ClientSocket::unnamed <unknown> > > <-- Como::ClientSocket::unnamed <unknown> > > <-- Como::ClientSocket::unnamed <unknown> > > <-- Como::ClientSocket::unnamed <unknown> > > <-- Como::ClientSocket::unnamed <unknown> > > > > > > Chris > > } > > AFAIK, the only problems with plugins is meta-type registration that > has to be done explicitly by calling qRegisterMataType(...); > I don't think that QMetaObject suffers from this. Maybe someone can > confirm this. > > - doesn't relies on RTTI, and is way faster than dynamic cast > > This is understandable. But with comparison with static cast there are no > benefits? > > My point was: dynamic_cast is safer than static_cast, and qobject_cast > is faster than dynamic_cast. > > So qobject_cast is a "double win" over static_cast and dynamic_cast. > > Chris > > Chris > > one? > > _______________________________________________ > Interest mailing list > Interest@qt-project.org > http://lists.qt-project.org/mailman/listinfo/interest > > _______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest