1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the documentation of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:FDL$
10 ** GNU Free Documentation License
11 ** Alternatively, this file may be used under the terms of the GNU Free
12 ** Documentation License version 1.3 as published by the Free Software
13 ** Foundation and appearing in the file included in the packaging of
17 ** Alternatively, this file may be used in accordance with the terms
18 ** and conditions contained in a signed written agreement between you
26 ****************************************************************************/
28 \page qdeclarativescope.html
29 \inqmlmodule QtQuick 1
34 QML property bindings, inline functions and imported JavaScript files all
35 run in a JavaScript scope. Scope controls which variables an expression can
36 access, and which variable takes precedence when two or more names conflict.
38 As JavaScript's built-in scope mechanism is very simple, QML enhances it to fit
39 more naturally with the QML language extensions.
41 \section1 JavaScript Scope
43 QML's scope extensions do not interfere with JavaScript's natural scoping.
44 JavaScript programmers can reuse their existing knowledge when programming
45 functions, property bindings or imported JavaScript files in QML.
47 In the following example, the \c {addConstant()} method will add 13 to the
48 parameter passed just as the programmer would expect irrespective of the
49 value of the QML object's \c a and \c b properties.
56 function addConstant(b) {
63 That QML respects JavaScript's normal scoping rules even applies in bindings.
64 This totally evil, abomination of a binding will assign 12 to the QML object's
75 Every JavaScript expression, function or file in QML has its own unique
76 variable object. Local variables declared in one will never conflict
77 with local variables declared in another.
79 \section1 Element Names and Imported JavaScript Files
81 \l {QML Document}s include import statements that define the element names
82 and JavaScript files visible to the document. In addition to their use in the
83 QML declaration itself, element names are used by JavaScript code when accessing
84 \l {Attached Properties} and enumeration values.
86 The effect of an import applies to every property binding, and JavaScript
87 function in the QML document, even those in nested inline components. The
88 following example shows a simple QML file that accesses some enumeration
89 values and calls an imported JavaScript function.
93 import "code.js" as Code
96 snapMode: ListView.SnapToItem
100 elide: Text.ElideMiddle
101 text: "A really, really long string that will require eliding."
102 color: Code.defaultColor()
108 \section1 Binding Scope Object
110 Property bindings are the most common use of JavaScript in QML. Property
111 bindings associate the result of a JavaScript expression with a property of an
112 object. The object to which the bound property belongs is known as the binding's
113 scope object. In this QML simple declaration the \l Item object is the
114 binding's scope object.
118 anchors.left: parent.left
122 Bindings have access to the scope object's properties without qualification.
123 In the previous example, the binding accesses the \l Item's \c parent property
124 directly, without needing any form of object prefix. QML introduces a more
125 structured, object-oriented approach to JavaScript, and consequently does not
126 require the use of the JavaScript \c this property.
128 Care must be used when accessing \l {Attached Properties} from bindings due
129 to their interaction with the scope object. Conceptually attached properties
130 exist on \e all objects, even if they only have an effect on a subset of those.
131 Consequently unqualified attached property reads will always resolve to an
132 attached property on the scope object, which is not always what the programmer
135 For example, the \l PathView element attaches interpolated value properties to
136 its delegates depending on their position in the path. As PathView only
137 meaningfully attaches these properties to the root element in the delegate, any
138 sub-element that accesses them must explicitly qualify the root object, as shown
143 delegate: Component {
147 scale: root.PathView.scale
154 If the \l Image element omitted the \c root prefix, it would inadvertently access
155 the unset \c {PathView.scale} attached property on itself.
157 \section1 Component Scope
159 Each QML component in a QML document defines a logical scope. Each document
160 has at least one root component, but can also have other inline sub-components.
161 The component scope is the union of the object ids within the component and the
162 component's root element's properties.
166 property string title
170 text: "<b>" + title + "</b>"
172 anchors.top: parent.top
176 text: titleElement.text
178 anchors.bottom: parent.bottom
183 The example above shows a simple QML component that displays a rich text title
184 string at the top, and a smaller copy of the same text at the bottom. The first
185 \c Text element directly accesses the component's \c title property when
186 forming the text to display. That the root element's properties are directly
187 accessible makes it trivial to distribute data throughout the component.
189 The second \c Text element uses an id to access the first's text directly. IDs
190 are specified explicitly by the QML programmer so they always take precedence
191 over other property names (except for those in the \l {JavaScript Scope}). For
192 example, in the unlikely event that the binding's \l {Binding Scope Object}{scope
193 object} had a \c titleElement property in the previous example, the \c titleElement
194 id would still take precedence.
196 \section1 Component Instance Hierarchy
198 In QML, component instances connect their component scopes together to form a
199 scope hierarchy. Component instances can directly access the component scopes of
202 The easiest way to demonstrate this is with inline sub-components whose component
203 scopes are implicitly scoped as children of the outer component.
207 property color defaultColor: "blue"
210 delegate: Component {
219 The component instance hierarchy allows instances of the delegate component
220 to access the \c defaultColor property of the \c Item element. Of course,
221 had the delegate component had a property called \c defaultColor that would
222 have taken precedence.
224 The component instance scope hierarchy extends to out-of-line components, too.
225 In the following example, the \c TitlePage.qml component creates two
226 \c TitleText instances. Even though the \c TitleText element is in a separate
227 file, it still has access to the \c title property when it is used from within
228 the \c TitlePage. QML is a dynamically scoped language - depending on where it
229 is used, the \c title property may resolve differently.
235 property string title
239 anchors.top: parent.top
244 anchors.bottom: parent.bottom
252 text: "<b>" + title + "</b>"
257 Dynamic scoping is very powerful, but it must be used cautiously to prevent
258 the behavior of QML code from becoming difficult to predict. In general it
259 should only be used in cases where the two components are already tightly
260 coupled in another way. When building reusable components, it is preferable
261 to use property interfaces, like this:
268 property string title
273 anchors.top: parent.top
279 anchors.bottom: parent.bottom
286 property string title
289 text: "<b>" + title + "</b>"
294 \section1 JavaScript Global Object
296 In addition to all the properties that a developer would normally expect on
297 the JavaScript global object, QML adds some custom extensions to make UI or
298 QML specific tasks a little easier. These extensions are described in the
299 \l {QML Global Object} documentation.
301 QML disallows element, id and property names that conflict with the properties
302 on the global object to prevent any confusion. Programmers can be confident
303 that \c Math.min(10, 9) will always work as expected!