diff options
| author | Ulf Hermann <ulf.hermann@qt.io> | 2025-01-10 09:55:12 +0100 |
|---|---|---|
| committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2025-01-12 13:17:57 +0000 |
| commit | 377f26c35d75ce5a8afc92662de6a7538c48bb2d (patch) | |
| tree | ef23f45c424a3ed05088f0aaf8f9784f25ca60f7 | |
| parent | d5139f5138b4d9bd984bb7eb6f9a02b5fd03eea2 (diff) | |
QtQml: Fix docs about QVariantList and QVariantMap
QVariantList does not have to be stored as literally Array, an
array-like that we also use in so many other places is entirely
adequate. Furthermore, you can update QVariantList properties in place
these days.
Pick-to: 6.5
Task-number: QTBUG-125289
Change-Id: Iacf45579b1db31d7644259f9a0e46e595fe7ecfc
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 892fbeffe23afb3ce0a48ca8357e18f14db649e9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 6e3ef191c615b1ef1983d9a6421f0ee36d93b71c)
| -rw-r--r-- | src/qml/doc/src/cppintegration/data.qdoc | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/src/qml/doc/src/cppintegration/data.qdoc b/src/qml/doc/src/cppintegration/data.qdoc index 4932943fc1..9b464d0b50 100644 --- a/src/qml/doc/src/cppintegration/data.qdoc +++ b/src/qml/doc/src/cppintegration/data.qdoc @@ -142,16 +142,23 @@ additional features. See the \l {qtqml-javascript-hostenvironment.html} {JavaScript Host Environment} for further details.) -\section2 QVariantList and QVariantMap to JavaScript Array and Object +\section2 QVariantList and QVariantMap to JavaScript Array-like and Object The QML engine provides automatic type conversion between QVariantList and -JavaScript arrays, and between QVariantMap and JavaScript objects. +JavaScript array-likes, and between QVariantMap and JavaScript objects. + +An \e{array-like}, in ECMAScript terms, is an object used like an array. The +array-likes produced by conversion from QVariantList (and other C++ sequential +containers) are not only that but also offer the common array methods +and automatically synchronize the \e length property. They can be used just +like JavaScript Arrays for any practical purposes. The \e{Array.isArray()} +method still returns false for them, though. For example, the function defined in QML below expects two arguments, an array and an object, and prints their contents using the standard JavaScript syntax for array and object item access. The C++ code below calls this function, passing a QVariantList and a QVariantMap, which are automatically -converted to JavaScript array and object values, repectively: +converted to JavaScript array-like and object values, respectively: \table \row @@ -177,23 +184,24 @@ type or method parameter, the value can be created as a JavaScript array or object in QML, and is automatically converted to a QVariantList or QVariantMap when it is passed to C++. -Mind that QVariantList and QVariantMap properties of C++ types are stored as -values and cannot be changed in place by QML code. You can only replace the -whole map or list, but not manipulate its contents. The following code does -not work if the property \c l is a QVariantList: +Since Qt 6.5, QVariantList properties of C++ types can be changed in place by +QML code. Mind, however, that this does not hold for QVariantMap properties of +C++ types. Those are stored as values and cannot be changed in place by QML +code. You can only replace the whole map, but not manipulate its contents. The +following code does not work if the property \c m is a QVariantMap: \code -MyListExposingItem { - l: [1, 2, 3] - Component.onCompleted: l[0] = 10 +MyMapExposingItem { + m: ({ one: 1 }) + Component.onCompleted: m.ten = 10 } \endcode The following code does work: \code -MyListExposingItem { - l: [1, 2, 3] - Component.onCompleted: l = [10, 2, 3] +MyMapExposingItem { + m: ({ one: 1 }) + Component.onCompleted: m = { one: 1, ten: 10 } } \endcode |
