diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index f64a1a3b08..c94fb17755 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -482,14 +482,17 @@ inline T qobject_cast(const QObject *object) template constexpr const char * qobject_interface_iid() = delete; - -inline const QBindingStorage *qGetBindingStorage(const QObject *o) +template inline T * +qobject_iid_cast(QObject *object, const char *IId = qobject_interface_iid()) { - return o->bindingStorage(); + return reinterpret_cast((object ? object->qt_metacast(IId) : nullptr)); } -inline QBindingStorage *qGetBindingStorage(QObject *o) +template inline std::enable_if_t::value, T *> +qobject_iid_cast(const QObject *object) { - return o->bindingStorage(); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) + QObject *o = const_cast(object); + return qobject_iid_cast>(o); } #if defined(Q_CLANG_QDOC) @@ -499,11 +502,20 @@ inline QBindingStorage *qGetBindingStorage(QObject *o) template <> constexpr const char *qobject_interface_iid() \ { return IId; } \ template <> inline IFace *qobject_cast(QObject *object) \ - { return reinterpret_cast((object ? object->qt_metacast(IId) : nullptr)); } \ + { return qobject_iid_cast(object); } \ template <> inline const IFace *qobject_cast(const QObject *object) \ - { return reinterpret_cast((object ? const_cast(object)->qt_metacast(IId) : nullptr)); } + { return qobject_iid_cast(object); } #endif // Q_MOC_RUN +inline const QBindingStorage *qGetBindingStorage(const QObject *o) +{ + return o->bindingStorage(); +} +inline QBindingStorage *qGetBindingStorage(QObject *o) +{ + return o->bindingStorage(); +} + #ifndef QT_NO_DEBUG_STREAM Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *); #endif