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
topic.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-topic.html
5\meta {keywords} {qmltopic}
6\title Integrating QML and JavaScript
7\brief Description of how to use JavaScript in QML applications
8
9The QML language uses a JSON-like syntax and allows various expressions and
10methods to be defined as JavaScript functions. It also allows users to import
11JavaScript files and use the functionality those imports provide.
12
13This allows developers and designers to leverage the knowledge they have of
14JavaScript to quickly develop both user-interfaces and application logic.
15
16\section1 JavaScript Expressions
17
18QML has a deep JavaScript integration, and allows \l{Signal Attributes}
19{signal handlers} and \l{Method Attributes}{methods} to be defined in JavaScript.
20Another core feature of QML is the ability to specify and enforce relationships
21between object properties using \l{Property Binding}{property bindings}, which
22are also defined using JavaScript.
23
24See the documentation page titled
25\l{qtqml-javascript-expressions.html}{JavaScript Expressions in QML Documents}
26for more information about using JavaScript expressions in QML.
27
28\section1 Dynamic QML Object Creation from JavaScript
29
30QML supports the dynamic creation of objects from within JavaScript. This is
31useful to delay instantiation of objects until necessary, thereby improving
32application startup time. It also allows visual objects to be dynamically
33created and added to the scene in reaction to user input or other events. This
34functionality can be used in two main ways.
35
36Object can be created dynamically from JavaScript in an imperative way using
37\l{qtqml-javascript-dynamicobjectcreation.html}{dynamic creation of objects}.
38This can be useful, for example, when QML is used as an application scripting
39language.
40
41\note When creating user interfaces, the preferred way of creating objects
42dynamically is to use declarative constructs as these integrate best with the
43QML engine and tooling. Various types exist to enable this functionality such
44as the \l{Loader}, \l{Instantiator}, \l{Repeater} types.
45
46
47\section1 JavaScript Resources
48
49Application logic defined in JavaScript functions may be separated into
50separate JavaScript files known as JavaScript resources. There are several
51different kinds of JavaScript resources, with different semantics.
52
53See the documentation page titled \l{qtqml-javascript-resources.html}
54{Defining JavaScript Resources In QML} for more information about defining JavaScript
55resources for QML.
56
57\section1 JavaScript Imports
58
59A QML document may import JavaScript resources, and JavaScript resources may
60import other JavaScript resources as well as QML modules. This allows an
61application developer to provide application logic in modular, self-contained
62files.
63
64See the documentation page titled
65\l{qtqml-javascript-imports.html}{Importing JavaScript Resources}
66for more information on how to import JavaScript resources and how to use the
67functionality they provide.
68
69\section1 JavaScript Host Environment
70
71The QML engine provides a JavaScript environment that has some differences to
72the JavaScript environment provided by a web browser. Certain limitations
73apply to code running in the environment, and the QML engine provides various
74objects in the root context which may be unfamiliar to JavaScript developers.
75
76These limitations and extensions are documented in the description of the
77\l{qtqml-javascript-hostenvironment.html}{JavaScript Host Environment} provided
78by the QML engine.
79
80There is also an in depth description of the
81\l{qtqml-javascript-memory.html}{memory management} employed by the JavaScript
82engine.
83
84\section1 Configuring the JavaScript engine
85
86For specific use cases you may want to override some of the parameters the
87JavaScript engine uses for handling memory and compiling JavaScript. See
88\l{qtqml-javascript-finetuning.html}{Configuring the JavaScript engine} for
89more information on these parameters.
90
91\section1 Making Applications Scriptable
92
93Beyond using JavaScript within QML documents, Qt also allows you to embed a
94JavaScript engine in C++ applications to make them scriptable. This allows
95extending application functionality using JavaScript without recompiling the
96application.
97
98See \l{Making Applications Scriptable} for detailed information about using
99the JavaScript engine in C++ applications, including how to expose QObject
100instances to scripts and security considerations.
101
102*/