From 677cd24de676f4fe8542a671357e968285739de5 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Thu, 16 Mar 2023 15:30:57 +0100 Subject: qmlobject_cast: Make it useful again The Qt 6 implementation of qmlobject_cast simply accessed the metaObject of the object (canConvert -> QQmlMetaObject::QQmlMetaObject(QObject*) -> QOject:metaObject). That would directly cause the creation of dynamic meta-objects, if they weren't created so far. Given that we still didn't get rid of the property-cache in QML, use it to optimize this code path: If the object has some associated QQmlData, retrieve it and check if we have a property cache. If we do, retrieve the first C++ meta object from it. We can safely ignore any dynamic meta-objects as T must be a static meta-object. Then do the normal inheritance check with the first C++ meta-object and T::staticMetaObject. If we don't have a property cache, fall back to the previous implementation (but without the QQmlMetaObject detour). Change-Id: Ie23d1a3dac6bf343ee2bc31e04c4592681ad9995 Reviewed-by: Shawn Rutledge --- src/qml/qml/qqmlglobal_p.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/qml/qml/qqmlglobal_p.h') diff --git a/src/qml/qml/qqmlglobal_p.h b/src/qml/qml/qqmlglobal_p.h index 462c399065..c3dbc697a3 100644 --- a/src/qml/qml/qqmlglobal_p.h +++ b/src/qml/qml/qqmlglobal_p.h @@ -122,6 +122,8 @@ do { \ QMetaObject::disconnect(sender, signalIdx, receiver, methodIdx); \ } while (0) +Q_QML_PRIVATE_EXPORT bool qmlobject_can_cast(QObject *object, const QMetaObject *mo); + /*! This method is identical to qobject_cast() except that it does not require lazy QMetaObjects to be built, so should be preferred in all QML code that might interact @@ -137,7 +139,9 @@ do { \ template T qmlobject_cast(QObject *object) { - if (object && QQmlMetaObject::canConvert(object, &reinterpret_cast(object)->staticMetaObject)) + if (!object) + return nullptr; + if (qmlobject_can_cast(object, &(std::remove_pointer_t::staticMetaObject))) return static_cast(object); else return nullptr; -- cgit v1.2.3