diff options
Diffstat (limited to 'doc/src/declarative/scope.qdoc')
-rw-r--r-- | doc/src/declarative/scope.qdoc | 122 |
1 files changed, 49 insertions, 73 deletions
diff --git a/doc/src/declarative/scope.qdoc b/doc/src/declarative/scope.qdoc index 3317037..9a9934a 100644 --- a/doc/src/declarative/scope.qdoc +++ b/doc/src/declarative/scope.qdoc @@ -24,41 +24,17 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ - -/* - - - -and requires extension to -fit naturally with QML. - - -JavaScript has only b -JavaScript has a very simple built in scope is very simple - -script, and the precede d - -and \l {Integrating JavaScript}{JavaScript} are executed in a scope chain -automatically established by QML when a component instance is constructed. QML is a \e {dynamically scoped} -language. Different object instances instantiated from the same component can exist in -different scope chains. - -\image qml-scope.png - - -*/ - /*! \page qdeclarativescope.html \title QML Scope \tableofcontents -QML property bindings, inline functions and imported JavaScript files all -run in a JavaScript scope. Scope controls which variables an expression can +QML property bindings, inline functions and imported JavaScript files all +run in a JavaScript scope. Scope controls which variables an expression can access, and which variable takes precedence when two or more names conflict. -As JavaScript's built-in scope mechanism is very simple, QML enhances it to fit +As JavaScript's built-in scope mechanism is very simple, QML enhances it to fit more naturally with the QML language extensions. \section1 JavaScript Scope @@ -67,8 +43,8 @@ QML's scope extensions do not interfere with JavaScript's natural scoping. JavaScript programmers can reuse their existing knowledge when programming functions, property bindings or imported JavaScript files in QML. -In the following example, the \c {addConstant()} method will add 13 to the -parameter passed just as the programmer would expect irrespective of the +In the following example, the \c {addConstant()} method will add 13 to the +parameter passed just as the programmer would expect irrespective of the value of the QML object's \c a and \c b properties. \code @@ -83,8 +59,8 @@ QtObject { } \endcode -That QML respects JavaScript's normal scoping rules even applies in bindings. -This totally evil, abomination of a binding will assign 12 to the QML object's +That QML respects JavaScript's normal scoping rules even applies in bindings. +This totally evil, abomination of a binding will assign 12 to the QML object's \c a property. \code @@ -101,13 +77,13 @@ with local variables declared in another. \section1 Element Names and Imported JavaScript Files -\l {QML Document}s include import statements that define the element names -and JavaScript files visible to the document. In addition to their use in the -QML declaration itself, element names are used by JavaScript code when accessing -\l {Attached Properties} and enumeration values. +\l {QML Document}s include import statements that define the element names +and JavaScript files visible to the document. In addition to their use in the +QML declaration itself, element names are used by JavaScript code when accessing +\l {Attached Properties} and enumeration values. -The effect of an import applies to every property binding, and JavaScript -function in the QML document, even those in nested inline components. The +The effect of an import applies to every property binding, and JavaScript +function in the QML document, even those in nested inline components. The following example shows a simple QML file that accesses some enumeration values and calls an imported JavaScript function. @@ -130,10 +106,10 @@ ListView { \section1 Binding Scope Object -Property bindings are the most common use of JavaScript in QML. Property +Property bindings are the most common use of JavaScript in QML. Property bindings associate the result of a JavaScript expression with a property of an -object. The object to which the bound property belongs is known as the binding's -scope object. In this QML simple declaration the \l Item object is the +object. The object to which the bound property belongs is known as the binding's +scope object. In this QML simple declaration the \l Item object is the binding's scope object. \code @@ -144,21 +120,21 @@ Item { Bindings have access to the scope object's properties without qualification. In the previous example, the binding accesses the \l Item's \c parent property -directly, without needing any form of object prefix. QML introduces a more -structured, object-oriented approach to JavaScript, and consequently does not +directly, without needing any form of object prefix. QML introduces a more +structured, object-oriented approach to JavaScript, and consequently does not require the use of the JavaScript \c this property. Care must be used when accessing \l {Attached Properties} from bindings due to their interaction with the scope object. Conceptually attached properties exist on \e all objects, even if they only have an effect on a subset of those. -Consequently unqualified attached property reads will always resolve to an -attached property on the scope object, which is not always what the programmer +Consequently unqualified attached property reads will always resolve to an +attached property on the scope object, which is not always what the programmer intended. -For example, the \l PathView element attaches interpolated value properties to +For example, the \l PathView element attaches interpolated value properties to its delegates depending on their position in the path. As PathView only -meaningfully attaches these properties to the root element in the delegate, any -sub-element that accesses them must explicitly qualify the root object, as shown +meaningfully attaches these properties to the root element in the delegate, any +sub-element that accesses them must explicitly qualify the root object, as shown below. \code @@ -181,7 +157,7 @@ the unset \c {PathView.scale} attached property on itself. Each QML component in a QML document defines a logical scope. Each document has at least one root component, but can also have other inline sub-components. -The component scope is the union of the object ids within the component and the +The component scope is the union of the object ids within the component and the component's root element's properties. \code @@ -195,7 +171,7 @@ Item { anchors.top: parent.top } - Text { + Text { text: titleElement.text font.pixelSize: 18 anchors.bottom: parent.bottom @@ -203,7 +179,7 @@ Item { } \endcode -The example above shows a simple QML component that displays a rich text title +The example above shows a simple QML component that displays a rich text title string at the top, and a smaller copy of the same text at the bottom. The first \c Text element directly accesses the component's \c title property when forming the text to display. That the root element's properties are directly @@ -211,18 +187,18 @@ accessible makes it trivial to distribute data throughout the component. The second \c Text element uses an id to access the first's text directly. IDs are specified explicitly by the QML programmer so they always take precedence -over other property names (except for those in the \l {JavaScript Scope}). For -example, in the unlikely event that the binding's \l {Binding Scope Object}{scope -object} had a \c titleElement property in the previous example, the \c titleElement +over other property names (except for those in the \l {JavaScript Scope}). For +example, in the unlikely event that the binding's \l {Binding Scope Object}{scope +object} had a \c titleElement property in the previous example, the \c titleElement id would still take precedence. \section1 Component Instance Hierarchy -In QML, component instances connect their component scopes together to form a -scope hierarchy. Component instances can directly access the component scopes of +In QML, component instances connect their component scopes together to form a +scope hierarchy. Component instances can directly access the component scopes of their ancestors. -The easiest way to demonstrate this is with inline sub-components whose component +The easiest way to demonstrate this is with inline sub-components whose component scopes are implicitly scoped as children of the outer component. \code @@ -239,16 +215,16 @@ Item { } \endcode -The component instance hierarchy allows instances of the delegate component +The component instance hierarchy allows instances of the delegate component to access the \c defaultColor property of the \c Item element. Of course, -had the delegate component had a property called \c defaultColor that would -have taken precedence. +had the delegate component had a property called \c defaultColor that would +have taken precedence. The component instance scope hierarchy extends to out-of-line components, too. -In the following example, the \c TitlePage.qml component creates two -\c TitleText instances. Even though the \c TitleText element is in a separate -file, it still has access to the \c title property when it is used from within -the \c TitlePage. QML is a dynamically scoped language - depending on where it +In the following example, the \c TitlePage.qml component creates two +\c TitleText instances. Even though the \c TitleText element is in a separate +file, it still has access to the \c title property when it is used from within +the \c TitlePage. QML is a dynamically scoped language - depending on where it is used, the \c title property may resolve differently. \code @@ -256,13 +232,13 @@ is used, the \c title property may resolve differently. import QtQuick 1.0 Item { property string title - - TitleText { + + TitleText { size: 22 anchors.top: parent.top } - TitleText { + TitleText { size: 18 anchors.bottom: parent.bottom } @@ -277,10 +253,10 @@ Text { } \endcode -Dynamic scoping is very powerful, but it must be used cautiously to prevent +Dynamic scoping is very powerful, but it must be used cautiously to prevent the behavior of QML code from becoming difficult to predict. In general it -should only be used in cases where the two components are already tightly -coupled in another way. When building reusable components, it is preferable +should only be used in cases where the two components are already tightly +coupled in another way. When building reusable components, it is preferable to use property interfaces, like this: \code @@ -289,14 +265,14 @@ import QtQuick 1.0 Item { id: root property string title - - TitleText { + + TitleText { title: root.title size: 22 anchors.top: parent.top } - TitleText { + TitleText { title: root.title size: 18 anchors.bottom: parent.bottom @@ -322,7 +298,7 @@ QML specific tasks a little easier. These extensions are described in the \l {QML Global Object} documentation. QML disallows element, id and property names that conflict with the properties -on the global object to prevent any confusion. Programmers can be confident +on the global object to prevent any confusion. Programmers can be confident that \c Math.min(10, 9) will always work as expected! */ |