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
9The program logic for a QML application may be defined in JavaScript. The
10JavaScript code may either be defined in-line in QML documents, or separated
11into JavaScript files (known as \c {JavaScript Resources} in QML).
12
13There are two different kinds of JavaScript resources which are supported in
14QML: code-behind implementation files, and shared (library) files. Both kinds
15of JavaScript resource may be \l{qtqml-javascript-imports.html}{imported} by
16other JavaScript resources, or included in \l{qtqml-modules-topic.html}
17{QML modules}.
18
19\section1 Code-Behind Implementation Resource
20
21Most JavaScript files imported into a QML document are stateful implementations
22for the QML document importing them. In these cases, each instance of the QML
23object type defined in the document requires a separate copy of the JavaScript
24objects and state in order to behave correctly.
25
26The default behavior when importing JavaScript files is to provide a unique,
27isolated copy for each QML component instance. If that JavaScript file does
28not import any resources or modules with a \c{.import} statement, its code will
29run in the same scope as the QML component instance and consequently can access
30and manipulate the objects and properties declared in that QML component.
31Otherwise, it will have its own unique scope, and objects and properties of the
32QML component should be passed to the functions of the JavaScript file as
33parameters if they are required.
34
35An example of a code-behind implementation resource follows:
36
37\qml
38// MyButton.qml
39import QtQuick 2.0
40import "my_button_impl.js" as Logic // A new instance of this JavaScript resource
41 // is loaded for each instance of Button.qml.
42
43Rectangle {
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
59var clickCount = 0; // this state is separate for each instance of MyButton
60function 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
70In general, simple logic should be defined in-line in the QML file, but more
71complex logic should be separated into code-behind implementation resources
72for maintainability and readability.
73
74\section1 Shared JavaScript Resources (Libraries)
75
76By default, JavaScript files imported from QML share their context with the QML
77component. That means the JavaScript files have access to the same QML objects
78and can modify them. As a consequence, each import must have a unique copy of
79these files.
80
81\l {Defining JavaScript Resources in QML#Code-Behind Implementation Resource}
82{The previous section} covers stateful imports of JavaScript files. However,
83some JavaScript files are stateless and act more like reusable libraries, in
84the sense that they provide a set of helper functions that do not require
85anything from where they were imported from. You can save significant amounts
86of memory and speed up the instantiation of QML components if you mark such
87libraries with a special pragma, as shown in the following example.
88
89\qml
90// factorial.js
91.pragma library
92
93var factorialCount = 0;
94
95function 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
109function factorialCallCount() {
110 return factorialCount;
111}
112\endqml
113
114The pragma declaration must appear before any JavaScript code excluding comments.
115
116Note that multiple QML documents can import \c{"factorial.js"} and call the
117factorial and factorialCallCount functions that it provides. The state of the
118JavaScript import is shared across the QML documents which import it, and thus
119the return value of the factorialCallCount function may be non-zero when called
120within a QML document which never calls the factorial function.
121
122For example:
123
124\qml
125// Calculator.qml
126import QtQuick 2.0
127import "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
132Text {
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
140As they are shared, .pragma library files cannot access QML component instance
141objects or properties directly, although QML values can be passed as function
142parameters.
143
144*/