diff options
Diffstat (limited to 'src/qml/jsapi/qjsvalue.cpp')
-rw-r--r-- | src/qml/jsapi/qjsvalue.cpp | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/qml/jsapi/qjsvalue.cpp b/src/qml/jsapi/qjsvalue.cpp index 0aaa471e23..f6b97262c3 100644 --- a/src/qml/jsapi/qjsvalue.cpp +++ b/src/qml/jsapi/qjsvalue.cpp @@ -19,7 +19,9 @@ #include <private/qv4mm_p.h> #include <private/qv4jscall_p.h> #include <private/qv4qobjectwrapper_p.h> +#include <private/qv4qmetaobjectwrapper_p.h> #include <private/qv4urlobject_p.h> +#include <private/qqmlbuiltins_p.h> /*! \since 5.0 @@ -163,10 +165,11 @@ /*! \enum QJSValue::ObjectConversionBehavior - This enum is used to specify how JavaScript objects without an equivalent + This enum is used to specify how JavaScript objects and symbols without an equivalent native Qt type should be treated when converting to QVariant. \value ConvertJSObjects A best-effort, possibly lossy, conversion is attempted. + Symbols are converted to QString. \value RetainJSObjects The value is retained as QJSValue wrapped in QVariant. */ @@ -371,8 +374,11 @@ bool QJSValue::isError() const } /*! - Returns true if this QJSValue is an object of the URL class; + Returns true if this QJSValue is an object of the URL JavaScript class; otherwise returns false. + + \note For a QJSValue that contains a QUrl, this function returns false. + However, \c{toVariant().value<QUrl>()} works in both cases. */ bool QJSValue::isUrl() const { @@ -446,16 +452,32 @@ bool QJSValue::isCallable() const return QJSValuePrivate::asManagedType<FunctionObject>(this); } +#if QT_DEPRECATED_SINCE(6, 9) /*! + \deprecated [6.9] Returns true if this QJSValue is a variant value; otherwise returns false. + \warning This function is likely to give unexpected results. + A variant value is only constructed by the QJSEngine in a very + limited number of cases. This used to be different before Qt + 5.14, where \l{QJSEngine::toScriptValue} would have created + them for more types instead of corresponding ECMAScript types. + You can get a valid \l QVariant via \l toVariant for many values + for which \c{isVariant} returns false. + \sa toVariant() */ bool QJSValue::isVariant() const { - return QJSValuePrivate::asManagedType<QV4::VariantObject>(this); + if (QJSValuePrivate::asManagedType<QV4::VariantObject>(this)) + return true; + if (auto vt = QJSValuePrivate::asManagedType<QV4::QQmlValueTypeWrapper>(this)) + if (vt->metaObject() == &QQmlVarForeign::staticMetaObject) + return true; + return false; } +#endif /*! Returns the string value of this QJSValue, as defined in @@ -634,8 +656,11 @@ QVariant QJSValue::toVariant(QJSValue::ObjectConversionBehavior behavior) const if (val.isString()) return QVariant(val.toQString()); if (val.as<QV4::Managed>()) { - return QV4::ExecutionEngine::toVariant( - val, /*typeHint*/ QMetaType{}, behavior == RetainJSObjects); + if (behavior == RetainJSObjects) + return QV4::ExecutionEngine::toVariant( + val, /*typeHint*/ QMetaType{}, /*createJSValueForObjectsAndSymbols=*/ true); + else + return QV4::ExecutionEngine::toVariantLossy(val); } Q_ASSERT(false); |