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
hostenvironment.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2017 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
/*!
4
\page qtqml-javascript-hostenvironment.html
5
\title JavaScript Host Environment
6
\brief Description of the JavaScript host environment provided by the QML engine
7
8
9
QML provides a JavaScript host environment tailored to writing QML applications.
10
This environment is different from the host environment provided by a browser
11
or a server-side JavaScript environment such as Node.js. For example, QML does
12
not provide a \c window object or \c{DOM API} as commonly found in a browser environment.
13
14
\section1 Common Base
15
16
Like a browser or server-side JavaScript environment, the QML runtime implements the
17
\l{ECMA-262}{ECMAScript Language Specification} standard. This provides access to
18
all of the built-in types and functions defined by the standard, such as Object, Array, and Math.
19
The QML runtime implements the 7th edition of the standard.
20
21
\l{Nullish Coalescing} (\c{??}) (since Qt 5.15) and \l{Optional Chaining} (\c{?.}) (since Qt 6.2)
22
are also implemented in the QML runtime.
23
24
Moreover, numeric literal separators (the underscores in \c 1_000_000) are supported since Qt 6.8.
25
26
The standard ECMAScript built-ins are not explicitly documented in the QML documentation. For more
27
information on their use, please refer to the ECMA-262 7th edition standard or one of the many online
28
JavaScript reference and tutorial sites, such as the \l{W3Schools JavaScript Reference} (JavaScript Objects
29
Reference section). Many sites focus on JavaScript in the browser, so in some cases you may need to double
30
check the specification to determine whether a given function or object is part of standard ECMAScript or
31
specific to the browser environment. In the case of the W3Schools link above, the \c{JavaScript Objects
32
Reference} section generally covers the standard, while the \c{Browser Objects Reference} and \c{HTML DOM
33
Objects Reference} sections are browser specific (and thus not applicable to QML).
34
35
\section1 Type annotations and assertions
36
37
Function declarations in QML documents can, and should, contain type
38
annotations. Type annotations are appended to the declaration of arguments and
39
to the function itself, for annotating the return type. The following function
40
takes an \c int and a \c string parameter, and returns a \c QtObject:
41
42
\qml
43
function doThings(a: int, b: string) : QtObject { ... }
44
\endqml
45
46
Type annotations help tools like \l{\QC Documentation}{\QC}
47
and \l{qmllint} to make sense
48
of the code and provide better diagnostics. Moreover, they make functions easier
49
to use from C++. See
50
\l {qtqml-cppintegration-interactqmlfromcpp.html}{Interacting with QML Objects from C++}
51
for more information.
52
53
The exception to this rule are functions assigned to signal handlers: There, type annotations are
54
forbidden to avoid a potential mismatch with the types of the signal. This does not cause issues for
55
tooling, as the signal already provides the necessary information.
56
57
\note In QML, enumerations are not types and can therefore not be used as type annotations. Their
58
underlying numeric type, int or double, should be used instead.
59
60
Type assertions (sometimes called \e as-casts) can also be used in order to cast an object to a
61
different object type. If the object is actually of the given type, then the type assertion returns
62
the same object. If not, it returns \c null. In the following snippet we assert that the \c parent
63
object is a \c Rectangle before accessing a specific member of it.
64
65
\qml
66
Item {
67
property color parentColor: (parent as Rectangle)?.color || "red"
68
}
69
\endqml
70
71
The optional chaining (\c{?.}) avoids throwing an exception if the parent is
72
actually not a rectangle. In that case "red" is chosen as \c parentColor.
73
74
Since Qt 6.7 type annotations are always enforced when calling functions. Values
75
are coerced to the required types as necessary. Previously, type annotations were
76
ignored by the interpreter and the JIT compiler, but enforced by \l{qmlcachegen}
77
and \l{qmlsc} when compiling to C++. This could lead to differences in behavior in
78
some corner cases. In order to explicitly request the old behavior of the
79
interpreter and JIT you can add the following to your QML document:
80
81
\qml
82
pragma FunctionSignatureBehavior: Ignored
83
\endqml
84
85
\section1 QML Global Object
86
87
The QML JavaScript host environment implements a number of host objects and functions, as
88
detailed in the \l{QML Global Object} documentation.
89
90
These host objects and functions are always available, regardless of whether any modules
91
have been imported.
92
93
94
\section1 JavaScript Objects and Functions
95
96
A list of the JavaScript objects, functions and properties supported by the
97
QML engine can be found in the \l{List of JavaScript Objects and Functions}.
98
99
Note that QML makes the following modifications to native objects:
100
101
\list
102
\li An \l {string}{arg()} function is added to the \c String prototype.
103
\li Locale-aware conversion functions are added to the \l Date and \l Number prototypes.
104
\endlist
105
106
In addition, QML also extends the behavior of the instanceof function to
107
allow for type checking against QML types. This means that you may use it to
108
verify that a variable is indeed the type you expect, for example:
109
110
\qml
111
var v = something();
112
if (!v instanceof Item) {
113
throw new TypeError("I need an Item type!");
114
}
115
116
...
117
\endqml
118
119
120
\section1 JavaScript Environment Restrictions
121
122
QML implements the following restrictions for JavaScript code:
123
124
\list
125
\li JavaScript code written in a \c .qml file cannot modify the global object.
126
JavaScript code in a .js file can modify the global object,
127
and those modifications will be visible to the .qml file when
128
\l {Importing a JavaScript Resource from a QML Document}{imported}.
129
130
In QML, the global object is constant - existing properties cannot be modified
131
or deleted, and no new properties may be created.
132
133
Most JavaScript programs do not intentionally modify the global object.
134
However, JavaScript's automatic creation of undeclared variables is an implicit
135
modification of the global object, and is prohibited in QML.
136
137
Assuming that the \c a variable does not exist in the scope chain, the
138
following code is illegal in QML:
139
140
\code
141
// Illegal modification of undeclared variable
142
a = 1;
143
for (var ii = 1; ii < 10; ++ii)
144
a = a * ii;
145
console.log("Result: " + a);
146
\endcode
147
148
It can be trivially modified to this legal code.
149
150
\code
151
var a = 1;
152
for (var ii = 1; ii < 10; ++ii)
153
a = a * ii;
154
console.log("Result: " + a);
155
\endcode
156
157
Any attempt to modify the global object - either implicitly or explicitly - will
158
cause an exception. If uncaught, this will result in a warning being printed,
159
that includes the file and line number of the offending code.
160
161
\li Global code is run in a reduced scope.
162
163
During startup, if a QML file includes an external JavaScript file with "global"
164
code, it is executed in a scope that contains only the external file itself and
165
the global object. That is, it will not have access to the QML objects and
166
properties it \l {Scope and Naming Resolution}{normally would}.
167
168
Global code that only accesses script local variables is permitted. This is an
169
example of valid global code.
170
171
\code
172
var colors = [ "red", "blue", "green", "orange", "purple" ];
173
\endcode
174
175
Global code that accesses QML objects will not run correctly.
176
177
\code
178
// Invalid global code - the "rootObject" variable is undefined
179
var initialPosition = { rootObject.x, rootObject.y }
180
\endcode
181
182
This restriction exists as the QML environment is not yet fully established.
183
To run code after the environment setup has completed, see
184
\l {JavaScript in Application Startup Code}.
185
186
\li The value of \c this is undefined in QML in the majority of contexts.
187
188
The \c this keyword is supported when binding properties from JavaScript.
189
In QML binding expressions, QML signal handlers, and QML declared functions,
190
\c this refers to the scope object. In all other situations, the value of
191
\c this is undefined in QML.
192
193
To refer to a specific object, provide an \c id. For example:
194
195
\qml
196
Item {
197
width: 200; height: 100
198
function mouseAreaClicked(area) {
199
console.log("Clicked in area at: " + area.x + ", " + area.y);
200
}
201
// This will pass area to the function
202
MouseArea {
203
id: area
204
y: 50; height: 50; width: 200
205
onClicked: mouseAreaClicked(area)
206
}
207
}
208
\endqml
209
210
\sa {Scope and Naming Resolution}
211
212
\endlist
213
214
215
216
*/
qtdeclarative
src
qml
doc
src
javascript
hostenvironment.qdoc
Generated on
for Qt by
1.14.0