qobject_cast is implemented this way:
template <class T>
inline T qobject_cast_helper
(QObject *object, T
) { return static_cast<T>(((T)0)->staticMetaObject.cast(object)); }
template <class T>
inline T qobject_cast
(QObject *object
) { return qobject_cast_helper<T>(object, T(0)); }
template <class T> inline T qobject_cast_helper(QObject *object, T)
{ return static_cast<T>(((T)0)->staticMetaObject.cast(object)); }
template <class T>
inline T qobject_cast(QObject *object)
{ return qobject_cast_helper<T>(object, T(0)); }
To copy to clipboard, switch view to plain text mode
So I guess it uses QMetaObject, whereas inherits() is simply:
inline bool inherits(const char *classname) const
{ return const_cast<QObject *>(this)->qt_metacast(classname) != 0; }
inline bool inherits(const char *classname) const
{ return const_cast<QObject *>(this)->qt_metacast(classname) != 0; }
To copy to clipboard, switch view to plain text mode
What's funny, qt_metacast() is defined by... Q_OBJECT macro... and implemented by moc:
void *MYCLASS::qt_metacast(const char *_clname)
{
if (!_clname) return 0;
if (!strcmp(_clname, qt_meta_stringdata_MYCLASS))
return static_cast<void*>(const_cast<MYCLASS*>(this));
if (!strcmp(_clname, "Ui::MYCLASS"))
return static_cast<Ui::MYCLASS*>(const_cast<MYCLASS*>(this));
return BASECLASS::qt_metacast(_clname);
}
void *MYCLASS::qt_metacast(const char *_clname)
{
if (!_clname) return 0;
if (!strcmp(_clname, qt_meta_stringdata_MYCLASS))
return static_cast<void*>(const_cast<MYCLASS*>(this));
if (!strcmp(_clname, "Ui::MYCLASS"))
return static_cast<Ui::MYCLASS*>(const_cast<MYCLASS*>(this));
return BASECLASS::qt_metacast(_clname);
}
To copy to clipboard, switch view to plain text mode
So... the bottom line is... if inherits() works for you, it's only because the base class (QObject?) has it implemented, but my guess is that it won't be possible to check if some subclass of MYCLASS inherits MYCLASS without the Q_OBJECT macro inside MYCLASS.
Bookmarks