Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qml-name-resolution.qdoc
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \internal
6 \page qml-name-resolution.html
7 \title Resolving Unqualified Names in QML
8
9 This page specifies how the QML engine and the QML compiler resolve unqualified names.
10
11 \section1 Terminology
12
13 \list
14 \li The \e location is the place in a document where the name to be resolved occurs.
15 \li An \e {unqualified name} is an identifier that is not the member of an object expression,
16 that is, not preceded by a \c{.}, and that no JavaScript declaration resolves. The
17 declarations are local variables, function parameters, and function names. They resolve
18 names lexically, before any of the rules below. In
19 \c{function f() { let Infinity = 42; return Infinity }}, \c Infinity is not an
20 unqualified name, and the function returns 42. In \c{property int i: width}, \c width is
21 an unqualified name.
22 \li An \e id is the name given to an object with an \c id property, such as \c root in
23 \c{id: root}. The engine stores the ids of a component as context properties of the
24 component's \c QQmlContext.
25 \li The \e {immediate scope object} is the object whose binding, signal handler, or function
26 contains the location. In the example below, it is the \c Rectangle for the binding on
27 \c color and the \c Text for the binding on \c text.
28 \li The \e {immediate context} is the \c QQmlContext of the component that contains the
29 location. Its \e {context object} is typically the root object of that component.
30 Often, the scope object and the context object are the same.
31 \li The \e {lexically enclosing contexts} are the contexts of the components in whose
32 definitions the location is nested, from the inside out. In the example below, the
33 delegate of \c Repeater is a component defined inside \c Outer.qml. A location inside the
34 delegate has the delegate context as immediate context. The \c Outer.qml context is
35 lexically enclosing. A context in which \c Outer or the delegate is merely instantiated
36 is not.
37 \endlist
38
39 \qml
40 // Outer.qml
41 Item { // context object of the Outer.qml context
42 id: outerRoot
43 Repeater {
44 delegate: Item { // context object of the delegate context
45 id: delegateRoot
46 Rectangle {
47 id: inner
48 color: delegateRoot.color // scope object: the Rectangle
49 Text { text: outerRoot.objectName } // scope object: the Text
50 }
51 }
52 }
53 }
54 \endqml
55
56 "Lookup" has a specific meaning in the engine, which is not invoked by these general rules.
57
58 \section1 Name Resolution Rules
59
60 The rules below are checked in order. A rule applies if the place it describes has a member of
61 the given name, no matter whether that member is a property, method, signal, id, type, import
62 namespace, or imported script. The first rule that applies decides. We don't need to know the
63 kind of a name beforehand. In particular, a method found in an earlier place shadows a property
64 of the same name in a later place, and the other way around.
65
66 \list 1
67 \li \target rule-keywords Keywords: The translation functions and the constants \c undefined, \c
68 Infinity, and \c NaN override everything else, in translation bindings as well as in calls
69 at run time.
70 \li \target rule-imports Imports: Types, import namespaces, and imported scripts of the document
71 containing the location. Upper-case type names are always found. Lower-case type names
72 (value types) are only found with \c{ValueTypeBehavior: Addressable}. The precedence between
73 different kinds of imports (explicit module, directory, or script; scoped or unscoped;
74 implicit; qmldir) and the impact of their ordering in a document shall be specified
75 separately.
76 \li \target rule-immediate-ids Ids of the immediate context. Only a QQmlExpression evaluated in
77 a context created by the user, for example for a script string, can find context properties
78 other than ids here.
79 \li \target rule-bound-ids With \c{ComponentBehavior: Bound}: Ids of the lexically enclosing
80 contexts, from the inside out. The chain ends at the root object of the document. Inline
81 components are exempt from \c{ComponentBehavior: Bound} because they can be instantiated
82 anywhere, for example as \c Doc.Inner in a different document. Therefore, the root object of
83 an inline component ends the chain, too. The context an inline component is instantiated in
84 is never lexically enclosing, even if it belongs to the same document. All of these contexts
85 are internal and can therefore not contain custom context properties. \c{ComponentBehavior:
86 Bound} causes Qt's own types not to insert any further contexts between a component and its
87 creation context.
88 \li \target rule-scope-object The immediate scope object.
89 \li \target rule-context-object The context object of the immediate context.
90 \li \target rule-bound-context-objects With \c{ComponentBehavior: Bound}: The context objects of
91 the lexically enclosing contexts, in the same order as in \l {rule-bound-ids}{rule 4}.
92 \li \target rule-global-object The remaining members of the JavaScript global object, see \l
93 {rule-keywords}{rule 1} for the others.
94 \li \target rule-remaining-contexts The remaining contexts, outside the ones covered in \l
95 {rule-immediate-ids}{rule 3}, \l {rule-bound-ids}{4}, \l {rule-context-object}{6}, and \l
96 {rule-bound-context-objects}{7}. For each of those, the context properties are searched
97 first, then the context object.
98 \endlist
99
100 All of this holds no matter if the code is compiled to C++ or interpreted, and no matter if
101 the \c{Lookups} are populated already.
102
103 \section1 Disappearing Scope and Context Objects
104
105 If a scope object or a context object disappears at run time, its type is still retained and
106 we can still determine whether it would have resolved the name in question. If so, a
107 ReferenceError is thrown. The properties of the object's context, typically ids, stay in
108 place. An id whose object is gone is null. A scope object can disappear while code using it as
109 scope is still alive, for example a function stored in a \c var property after the object it
110 is defined in has been destroyed.
111
112 \section1 Internal, Frozen, and User-Created Contexts
113
114 The contexts created by the QML engine for the components it instantiates are internal. They
115 cannot gain or lose properties and their context objects cannot be replaced.
116
117 Qt's own types that insert contexts between an unbound component and its creation context
118 (Loader, the delegate models, ListView's section delegates, PageIndicator's delegates, and some
119 dialog implementations) shall freeze those contexts after filling them, using a new public
120 method on QQmlContext. A frozen context behaves like an internal one.
121
122 User code can still insert contexts that are not frozen between an unbound component and its
123 creation context, using a QQmlContext and QQmlComponent::create() or similar. Such contexts are
124 only searched in \l {rule-remaining-contexts}{rule 9}, and replacements of their context objects
125 can be of a different type. We cannot see such contexts ahead of time. Therefore, qmlcachegen
126 compiles code as if they were not present.
127
128 \section1 Dynamic Metaobjects
129
130 A scope object or context object with a dynamic metaobject can gain and lose names at any
131 time. The only public way to create such an object is QQmlPropertyMap. Qt's own model items
132 also have dynamic metaobjects.
133
134 \section1 Caching and Compiling Ahead of Time
135
136 Based on the rules above, the following guidelines apply to caching and compiling ahead of
137 time:
138
139 \list
140 \li Keywords, types, and the global object cannot change. They can be compiled ahead of time
141 and cached in lookups. Ids, properties, and methods declared in QML cannot be named like
142 members of the global object.
143 \li The ids of a document are fixed. A context with ids is internal and cannot gain or lose
144 properties. Therefore, the ids, including the enclosing ids of a bound component, can be
145 compiled ahead of time and cached.
146 \li The types of the scope object and the context object are known when compiling ahead of
147 time. Only script strings and PropertyChanges can run the same code with a different scope
148 object or context object, and those are not compiled to C++. On the lookup side, deep
149 lookups into the context hierarchy are only cached as
150 \c ContextGetterInParentContextHierarchy anyway and those are repeated each time.
151 Therefore, checking the top context or scope object for changes is enough. If we detect an
152 object with a dynamic metaobject or a non-internal immediate context, we can't cache
153 anything beyond those since they might gain or lose properties at any time, invalidating
154 previous results.
155 \li The lexically enclosing contexts of a bound component are internal, and their context
156 objects are known when compiling ahead of time. In principle we could cache and compile
157 them. Where necessary this can be added. The compiler has to check them before resolving a
158 name to the global object, though.
159 \li Only QQmlExpression, and therefore script strings, can run in a non-internal immediate
160 context (that is, a user-created context).
161 \li Anything found on or beyond an object with a dynamic metaobject cannot be compiled ahead
162 of time. The compiler only knows its static properties. It can recognize QQmlPropertyMap.
163 \li The remaining contexts of \l {rule-remaining-contexts}{rule 9} come after the global object.
164 Frozen and internal ones could be cached, but their context objects may have dynamic
165 metaobjects, for example the model items of a delegate model. Anything beyond a context that
166 is neither frozen nor internal cannot be cached.
167 \endlist
168*/