aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2023-06-21 14:04:12 +0200
committerUlf Hermann <ulf.hermann@qt.io>2023-06-22 08:23:34 +0200
commit6654637a46eda100f20eec9146a9dafb232ec0a9 (patch)
tree7ad88fcc7e1ca63d82b6a5b92d78d6bc7e7dbab1 /src
parent7a3448a8e4c56d6adc40ef5887c2f12ce7b68d64 (diff)
QtQml: Rename qmlobject_can_cast
We want a version that only casts using the first C++ metaobject, and another one that takes the full QML type hierarchy into account. Change-Id: Ie23cf774f7837955de63d5ccb6c7cead1d1948f2 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/qml/jsruntime/qv4qobjectwrapper.cpp4
-rw-r--r--src/qml/qml/qqmlglobal.cpp23
-rw-r--r--src/qml/qml/qqmlglobal_p.h5
3 files changed, 23 insertions, 9 deletions
diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp
index e16b347ded..b891534712 100644
--- a/src/qml/jsruntime/qv4qobjectwrapper.cpp
+++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp
@@ -1672,7 +1672,7 @@ static int MatchScore(const Value &actual, QMetaType conversionMetaType)
QObject *wrapped = wrapper->object();
if (!wrapped)
return 0;
- if (qmlobject_can_cast(wrapped, conversionMetaType.metaObject()))
+ if (qmlobject_can_cpp_cast(wrapped, conversionMetaType.metaObject()))
return 0;
}
}
@@ -1693,7 +1693,7 @@ static int MatchScore(const Value &actual, QMetaType conversionMetaType)
}
} else if (QObject *object = wrapper->object()) {
if (conversionMetaType.flags() & QMetaType::PointerToQObject
- && qmlobject_can_cast(object, conversionMetaType.metaObject())) {
+ && qmlobject_can_cpp_cast(object, conversionMetaType.metaObject())) {
return 0;
}
}
diff --git a/src/qml/qml/qqmlglobal.cpp b/src/qml/qml/qqmlglobal.cpp
index 3759983b7a..232cffd7fe 100644
--- a/src/qml/qml/qqmlglobal.cpp
+++ b/src/qml/qml/qqmlglobal.cpp
@@ -850,14 +850,27 @@ void QQmlApplication::setDomain(const QString &arg)
QCoreApplication::instance()->setOrganizationDomain(arg);
}
-bool qmlobject_can_cast(QObject *object, const QMetaObject *mo)
+static const QQmlData *ddata_for_cast(QObject *object)
{
Q_ASSERT(object);
- Q_ASSERT(mo);
auto ddata = QQmlData::get(object, false);
- if (!ddata || ! ddata->propertyCache)
- return object->metaObject()->inherits(mo);
- return ddata->propertyCache->firstCppMetaObject()->inherits(mo);
+ return (ddata && ddata->propertyCache) ? ddata : nullptr;
+}
+
+bool qmlobject_can_cpp_cast(QObject *object, const QMetaObject *mo)
+{
+ Q_ASSERT(mo);
+ if (const QQmlData *ddata = ddata_for_cast(object))
+ return ddata->propertyCache->firstCppMetaObject()->inherits(mo);
+ return object->metaObject()->inherits(mo);
+}
+
+bool qmlobject_can_qml_cast(QObject *object, const QMetaObject *mo)
+{
+ Q_ASSERT(mo);
+ if (const QQmlData *ddata = ddata_for_cast(object))
+ return ddata->propertyCache->metaObject()->inherits(mo);
+ return object->metaObject()->inherits(mo);
}
QT_END_NAMESPACE
diff --git a/src/qml/qml/qqmlglobal_p.h b/src/qml/qml/qqmlglobal_p.h
index bb710b059d..ce18cfb1e1 100644
--- a/src/qml/qml/qqmlglobal_p.h
+++ b/src/qml/qml/qqmlglobal_p.h
@@ -122,7 +122,8 @@ do { \
QMetaObject::disconnect(sender, signalIdx, receiver, methodIdx); \
} while (0)
-Q_QML_PRIVATE_EXPORT bool qmlobject_can_cast(QObject *object, const QMetaObject *mo);
+Q_QML_PRIVATE_EXPORT bool qmlobject_can_cpp_cast(QObject *object, const QMetaObject *mo);
+Q_QML_PRIVATE_EXPORT bool qmlobject_can_qml_cast(QObject *object, const QMetaObject *mo);
/*!
This method is identical to qobject_cast<T>() except that it does not require lazy
@@ -141,7 +142,7 @@ T qmlobject_cast(QObject *object)
{
if (!object)
return nullptr;
- if (qmlobject_can_cast(object, &(std::remove_pointer_t<T>::staticMetaObject)))
+ if (qmlobject_can_cpp_cast(object, &(std::remove_pointer_t<T>::staticMetaObject)))
return static_cast<T>(object);
else
return nullptr;