diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-11-15 11:57:47 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-11-22 16:17:15 +0100 |
commit | 0925c51c5988bee7ee96944de2d857b2132ebe65 (patch) | |
tree | b0b2f411ffa9dacc81cd90f8f35621ff4fa063b0 /src/qml/jsapi | |
parent | c58d2e5229c39e8007ce5b93822c66af5e765a50 (diff) |
QJSEngine: Optimize conversion from QObject* to QString
This is commonly done for logging. With this in place we can have the
code generator use coerceType() for such constructs.
[ChangeLog][QML][Important Behavior Changes] You can implement custom
toString() methods for your QML objects in JavaScript or in C++. Those
methods don't actually have to return a string. Previously, whatever
return value the method generated was forwarded as-is. Now it is coerced
to a string.
Change-Id: I4a9721a6948be0c24a36b31d453a74bd747db729
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Diffstat (limited to 'src/qml/jsapi')
-rw-r--r-- | src/qml/jsapi/qjsengine.cpp | 6 | ||||
-rw-r--r-- | src/qml/jsapi/qjsengine.h | 13 |
2 files changed, 19 insertions, 0 deletions
diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp index c15b5c1ccd..8b0b650c1f 100644 --- a/src/qml/jsapi/qjsengine.cpp +++ b/src/qml/jsapi/qjsengine.cpp @@ -920,6 +920,12 @@ bool QJSEngine::convertMetaType(QMetaType fromType, const void *from, QMetaType return QV4::ExecutionEngine::metaTypeFromJS(handle()->fromData(fromType, from), toType, to); } +QString QJSEngine::convertQObjectToString(QObject *object) +{ + return QV4::QObjectWrapper::objectToString( + handle(), object ? object->metaObject() : nullptr, object); +} + /*! \fn template <typename T> QJSValue QJSEngine::toScriptValue(const T &value) Creates a QJSValue with the given \a value. diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h index db78e1b9a9..9af6fd6e7c 100644 --- a/src/qml/jsapi/qjsengine.h +++ b/src/qml/jsapi/qjsengine.h @@ -101,6 +101,13 @@ public: if constexpr (std::is_same_v<T, QJSManagedValue>) return toManagedValue(value); + if constexpr (std::is_same_v<T, QString>) { + if (targetType.flags() & QMetaType::PointerToQObject) { + return convertQObjectToString( + *reinterpret_cast<QObject *const *>(value.constData())); + } + } + if (sourceType == QMetaType::fromType<QJSValue>()) return fromScriptValue<T>(*reinterpret_cast<const QJSValue *>(value.constData())); @@ -148,6 +155,11 @@ public: if constexpr (std::is_same_v<To, QVariant>) return QVariant::fromValue(from); + if constexpr (std::is_same_v<To, QString> + && std::is_base_of_v<QObject, std::remove_const_t<std::remove_pointer_t<To>>>) { + return convertQObjectToString(from); + } + if constexpr (std::is_same_v<To, std::remove_const_t<std::remove_pointer_t<To>> const *>) { using nonConstTo = std::remove_const_t<std::remove_pointer_t<To>> *; if constexpr (std::is_same_v<From, nonConstTo>) @@ -220,6 +232,7 @@ private: bool convertVariant(const QVariant &value, QMetaType metaType, void *ptr); bool convertMetaType(QMetaType fromType, const void *from, QMetaType toType, void *to); + QString convertQObjectToString(QObject *object); template<typename T> friend inline T qjsvalue_cast(const QJSValue &); |