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
qtqml-qtquick-compiler-tech.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2022 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtqml-qtquick-compiler-tech.html
6
\ingroup overviews
7
\title Qt Quick Compiler
8
\brief Overview of Qt Quick Compiler components
9
10
Qt Quick Compiler lets you process QML and JavaScript code at compile
11
time, rather than at run time. This allows for:
12
13
\list
14
\li Faster application startup
15
\li Faster evaluation of bindings and functions
16
\endlist
17
18
The Qt Quick Compiler consist of two components:
19
\list
20
\li \l {QML type compiler}
21
\li \l {QML script compiler}
22
\endlist
23
24
\note \e qmltc, \e qmlsc and \l qmlcachegen are internal build tools. If you
25
need to care about their invocation, you are either writing a build system, or
26
you are doing something wrong.
27
28
\section1 The QML type compiler
29
30
The \l{QML type compiler}, \e(qmltc) compiles QML types to C++ classes. These C++
31
classes are then added to your application and can be instantiated from other
32
C++ code. This way you can avoid much of the overhead of using \l{QQmlComponent}
33
to create instances of your QML types. In order to benefit from \l qmltc, you
34
need to adapt your C++ code and make use of the new classes.
35
36
\l qmltc can only compile a QML document if it completely understands its
37
structure. It will fail if an unsupported language feature is encountered.
38
It does not have to understand the JavaScript code in bindings and functions,
39
though.
40
41
\section1 The QML script compiler
42
43
The \l{QML script compiler}, (\e qmlsc and \e qmlcachegen) compiles bindings and
44
functions to both, an efficient byte code and C++ functions. This process
45
automatically happens behind the scenes if you are using \l{qt_add_qml_module}
46
to specify your QML modules. For more information about available options to
47
control different aspects of QML compilation, see
48
\l {Caching compiled QML sources}.
49
50
At compile time, for each QML or JavaScript
51
document a corresponding C++ file is created and built into the application. The
52
C++ file then contains a \e{QML compilation unit}, which consists of:
53
54
\list
55
\li An efficient representation of the document structure
56
\li Byte code for all functions and bindings in the document
57
\li C++ code for functions and bindings the compiler fully understands
58
\endlist
59
60
The QML engine then refrains from compiling the QML or JavaScript source code
61
at run time and instead uses the pre-built compilation unit to load the QML
62
component and its functions and bindings more quickly. The functions and
63
bindings that were compiled to C++ can also be executed faster. Other bindings
64
and functions are either interpreted directly from the byte code, or compiled
65
to machine code via a JIT compilation step at run time. At compile time, more
66
involved type analysis can be performed. Therefore, the generated C++ code
67
is generally more efficient than the result of the JIT compilation.
68
69
There are limitations on what JavaScript constructs can be compiled to C++.
70
For more information on these limitations, see
71
\l {Limitations when compiling JavaScript to C++}. For statistics on how well
72
a QML project can be compiled, see
73
\l {Obtaining statistics about the compilation of functions and bindings}.
74
75
76
\e{qmlsc} will be used instead of \e{qmlcachegen} if the Qt Quick Compiler
77
Extensions are installed. It has the following additional features over
78
\e{qmlcachegen}:
79
80
\list
81
\li It can compile documents in Direct Mode. In that case, the C++ headers
82
of the types underpinning other QML components are directly included
83
and the methods of those types are directly called. Conversely, in
84
Indirect Mode, \e qmlcachegen or \e qmlsc call methods through the
85
lookup mechanism which is also used in the interpreter and JIT.
86
\li It can compile documents in Static Mode. In that case, \e qmlsc assumes
87
that no properties of any types exposed to C++ can be shadowed by
88
derived types. This allows for more bindings and functions to be
89
compiled, but generates invalid code if any properties are shadowed.
90
\endlist
91
92
Instead of producing C++ as output, \l {qmlsc} and \l {qmlcachegen} can also
93
generate .qmlc, .jsc and .mjsc "cache files". These still contain a QML
94
compilation unit each, and can be loaded by the QML engine to avoid
95
re-compilation. They can only contain document structure and byte code, though.
96
Compilation of bindings and functions to C++ is omitted if cache files are
97
produced. Neither the CMake nor the qmake build system offered by Qt expose this
98
functionality.
99
100
\section1 Summary
101
102
The following table summarizes the differences between \l{qmltc},
103
\l{qmlcachegen} and \l{qmlsc}:
104
105
\table
106
\header
107
\li \e qmltc
108
\li \e qmlcachegen
109
\li \e qmlsc
110
\row
111
\li Compiles QML types to C++ classes
112
\li Compiles QML documents to QML compilation units
113
\li Compiles QML documents to QML compilation units
114
\row
115
\li Generated output acts as faster alternative to
116
\l{QQmlComponent}-based object creation.
117
\li Generated output is used internally by the QML engine to avoid
118
re-compilation, and to speed up execution.
119
\li Generated output is used internally by the QML engine to avoid
120
re-compilation, and to speed up execution. Direct Mode and Static
121
Mode can further accelerate your application.
122
\row
123
\li Available for all versions of Qt
124
\li Available for all versions of Qt
125
\li Available for commercial customers
126
\endtable
127
*/
qtdeclarative
src
qml
doc
src
qtqml-qtquick-compiler-tech.qdoc
Generated on
for Qt by
1.14.0