aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/doc/src
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2023-11-22 12:53:33 +0100
committerUlf Hermann <ulf.hermann@qt.io>2023-12-04 15:19:09 +0100
commit84d950bc32aa63ffa727dd8aa6bf8a65d6154e1f (patch)
tree00035aba604c6da66a8fcd91290911a50ca643e8 /src/qml/doc/src
parentf187c3e5c3261d4040e4c742e32d31025409063b (diff)
QML: Let IDs in outer context override bound components' properties
This is necessary to make the usage of such IDs actually safe. If we let local properties override outer IDs, then adding local properties in later versions invalidates the ID lookups. [ChangeLog][QtQml][Important Behavior Changes] In QML documents with bound components, IDs defined in outer contexts override properties defined in inner contexts now. This is how qmlcachegen has always interpreted bound components when generating C++ code, and it is required to make access to outer IDs actually safe. The interpreter and JIT have previously preferred inner properties over outer IDs. Pick-to: 6.6 6.5 Fixes: QTBUG-119162 Change-Id: Ic5d3cc3342b4518d3fde1b800efe1b95d8e8b210 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/doc/src')
-rw-r--r--src/qml/doc/src/qmllanguageref/documents/structure.qdoc54
1 files changed, 45 insertions, 9 deletions
diff --git a/src/qml/doc/src/qmllanguageref/documents/structure.qdoc b/src/qml/doc/src/qmllanguageref/documents/structure.qdoc
index c85dc53c1f..c2453e1cb5 100644
--- a/src/qml/doc/src/qmllanguageref/documents/structure.qdoc
+++ b/src/qml/doc/src/qmllanguageref/documents/structure.qdoc
@@ -64,13 +64,23 @@ class declaration.
\section2 ComponentBehavior
-With this pragma you can restrict components defined in this file to only
-create objects within their original context. This holds for inline
-components as well as Component elements explicitly or implicitly created
-as properties. If a component is bound to its context, you can safely
-use IDs from the rest of the file within the component. Otherwise, the
-engine and the QML tooling cannot know in advance what type, if any, such
-IDs will resolve to at run time.
+You may have multiple components defined in the same QML file. The root
+scope of the QML file is a component, and you may additionally have
+elements of type \l QQmlComponent, explicitly or implicitly created
+as properties, or inline components. Those components are nested. Each
+of the inner components is within one specific outer component. Most of
+the time, IDs defined in an outer component are accessible within all
+its nested inner components. You can, however, create elements from a
+component in any a different context, with different IDs available.
+Doing so breaks the assumption that outer IDs are available. Therefore,
+the engine and the QML tooling cannot generally know in advance what
+type, if any, such IDs will resolve to at run time.
+
+With the ComponentBehavior pragma you can restrict all inner components
+defined in a file to only create objects within their original context.
+If a component is bound to its context, you can safely use IDs from
+outer components in the same file within the component. QML tooling will
+then assume the outer IDs with their specific types to be available.
In order to bind the components to their context specify the \c{Bound}
argument:
@@ -79,8 +89,34 @@ argument:
pragma ComponentBehavior: Bound
\endqml
-The default is \c{Unbound}. You can also specify it explicitly. In a
-future version of Qt the default will change to \c{Bound}.
+This implies that, in case of name clashes, IDs defined outside a bound
+component override local properties of objects created from the
+component. Otherwise it wouldn't actually be safe to use the IDs since
+later versions of a module might add more properties to the component.
+If the component is not bound, local properties override IDs defined
+outside the component, but not IDs defined inside the component.
+
+The example below prints the \e r property of the ListView object with
+the id \e color, not the \e r property of the rectangle's color.
+
+\qml
+pragma ComponentBehavior: Bound
+import QtQuick
+
+ListView {
+ id: color
+ property int r: 12
+ model: 1
+
+ delegate: Rectangle {
+ Component.onCompleted: console.log(color.r)
+ }
+}
+\endqml
+
+The default value of \c ComponentBehavior is \c{Unbound}. You can also
+specify it explicitly. In a future version of Qt the default will change
+to \c{Bound}.
Delegate components bound to their context don't receive their own
private contexts on instantiation. This means that model data can only