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
resources.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-resources.html
5
\meta {keywords} {qmltopic}
6
\title Defining JavaScript Resources in QML
7
\brief Description of how JavaScript files may be defined for use in QML
8
9
The program logic for a QML application may be defined in JavaScript. The
10
JavaScript code may either be defined in-line in QML documents, or separated
11
into JavaScript files (known as \c {JavaScript Resources} in QML).
12
13
There are two different kinds of JavaScript resources which are supported in
14
QML: code-behind implementation files, and shared (library) files. Both kinds
15
of JavaScript resource may be \l{qtqml-javascript-imports.html}{imported} by
16
other JavaScript resources, or included in \l{qtqml-modules-topic.html}
17
{QML modules}.
18
19
\section1 Code-Behind Implementation Resource
20
21
Most JavaScript files imported into a QML document are stateful implementations
22
for the QML document importing them. In these cases, each instance of the QML
23
object type defined in the document requires a separate copy of the JavaScript
24
objects and state in order to behave correctly.
25
26
The default behavior when importing JavaScript files is to provide a unique,
27
isolated copy for each QML component instance. If that JavaScript file does
28
not import any resources or modules with a \c{.import} statement, its code will
29
run in the same scope as the QML component instance and consequently can access
30
and manipulate the objects and properties declared in that QML component.
31
Otherwise, it will have its own unique scope, and objects and properties of the
32
QML component should be passed to the functions of the JavaScript file as
33
parameters if they are required.
34
35
An example of a code-behind implementation resource follows:
36
37
\qml
38
// MyButton.qml
39
import QtQuick 2.0
40
import "my_button_impl.js" as Logic // A new instance of this JavaScript resource
41
// is loaded for each instance of Button.qml.
42
43
Rectangle {
44
id: rect
45
width: 200
46
height: 100
47
color: "red"
48
49
MouseArea {
50
id: mousearea
51
anchors.fill: parent
52
onClicked: Logic.onClicked(rect)
53
}
54
}
55
\endqml
56
57
\qml
58
// my_button_impl.js
59
var clickCount = 0; // this state is separate for each instance of MyButton
60
function onClicked(button) {
61
clickCount += 1;
62
if ((clickCount % 5) == 0) {
63
button.color = Qt.rgba(1,0,0,1);
64
} else {
65
button.color = Qt.rgba(0,1,0,1);
66
}
67
}
68
\endqml
69
70
In general, simple logic should be defined in-line in the QML file, but more
71
complex logic should be separated into code-behind implementation resources
72
for maintainability and readability.
73
74
\section1 Shared JavaScript Resources (Libraries)
75
76
By default, JavaScript files imported from QML share their context with the QML
77
component. That means the JavaScript files have access to the same QML objects
78
and can modify them. As a consequence, each import must have a unique copy of
79
these files.
80
81
\l {Defining JavaScript Resources in QML#Code-Behind Implementation Resource}
82
{The previous section} covers stateful imports of JavaScript files. However,
83
some JavaScript files are stateless and act more like reusable libraries, in
84
the sense that they provide a set of helper functions that do not require
85
anything from where they were imported from. You can save significant amounts
86
of memory and speed up the instantiation of QML components if you mark such
87
libraries with a special pragma, as shown in the following example.
88
89
\qml
90
// factorial.js
91
.pragma library
92
93
var factorialCount = 0;
94
95
function factorial(a) {
96
a = parseInt(a);
97
98
// factorial recursion
99
if (a > 0)
100
return a * factorial(a - 1);
101
102
// shared state
103
factorialCount += 1;
104
105
// recursion base-case.
106
return 1;
107
}
108
109
function factorialCallCount() {
110
return factorialCount;
111
}
112
\endqml
113
114
The pragma declaration must appear before any JavaScript code excluding comments.
115
116
Note that multiple QML documents can import \c{"factorial.js"} and call the
117
factorial and factorialCallCount functions that it provides. The state of the
118
JavaScript import is shared across the QML documents which import it, and thus
119
the return value of the factorialCallCount function may be non-zero when called
120
within a QML document which never calls the factorial function.
121
122
For example:
123
124
\qml
125
// Calculator.qml
126
import QtQuick 2.0
127
import "factorial.js" as FactorialCalculator // This JavaScript resource is only
128
// ever loaded once by the engine,
129
// even if multiple instances of
130
// Calculator.qml are created.
131
132
Text {
133
width: 500
134
height: 100
135
property int input: 17
136
text: "The factorial of " + input + " is: " + FactorialCalculator.factorial(input)
137
}
138
\endqml
139
140
As they are shared, .pragma library files cannot access QML component instance
141
objects or properties directly, although QML values can be passed as function
142
parameters.
143
144
*/
qtdeclarative
src
qml
doc
src
javascript
resources.qdoc
Generated on
for Qt by
1.16.1