diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2023-03-16 15:30:57 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2023-03-17 14:01:14 +0000 |
commit | 677cd24de676f4fe8542a671357e968285739de5 (patch) | |
tree | 28fe810060944697b0006a3c4c695e96e376e255 /src/qml/qml/qqmlglobal_p.h | |
parent | 51c6db3f3d9843faa7ab3a1839bce17cd4418bd2 (diff) |
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 <shawn.rutledge@qt.io>
Diffstat (limited to 'src/qml/qml/qqmlglobal_p.h')
-rw-r--r-- | src/qml/qml/qqmlglobal_p.h | 6 |
1 files changed, 5 insertions, 1 deletions
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<T>() 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<class T> T qmlobject_cast(QObject *object) { - if (object && QQmlMetaObject::canConvert(object, &reinterpret_cast<T>(object)->staticMetaObject)) + if (!object) + return nullptr; + if (qmlobject_can_cast(object, &(std::remove_pointer_t<T>::staticMetaObject))) return static_cast<T>(object); else return nullptr; |