Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
10Qt Quick Compiler lets you process QML and JavaScript code at compile
11time, 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
18The 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
25need to care about their invocation, you are either writing a build system, or
26you are doing something wrong.
27
28\section1 The QML type compiler
29
30The \l{QML type compiler}, \e(qmltc) compiles QML types to C++ classes. These C++
31classes are then added to your application and can be instantiated from other
32C++ code. This way you can avoid much of the overhead of using \l{QQmlComponent}
33to create instances of your QML types. In order to benefit from \l qmltc, you
34need 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
37structure. It will fail if an unsupported language feature is encountered.
38It does not have to understand the JavaScript code in bindings and functions,
39though.
40
41\section1 The QML script compiler
42
43The \l{QML script compiler}, (\e qmlsc and \e qmlcachegen) compiles bindings and
44functions to both, an efficient byte code and C++ functions. This process
45automatically happens behind the scenes if you are using \l{qt_add_qml_module}
46to specify your QML modules. For more information about available options to
47control different aspects of QML compilation, see
48\l {Caching compiled QML sources}.
49
50At compile time, for each QML or JavaScript
51document a corresponding C++ file is created and built into the application. The
52C++ 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
60The QML engine then refrains from compiling the QML or JavaScript source code
61at run time and instead uses the pre-built compilation unit to load the QML
62component and its functions and bindings more quickly. The functions and
63bindings that were compiled to C++ can also be executed faster. Other bindings
64and functions are either interpreted directly from the byte code, or compiled
65to machine code via a JIT compilation step at run time. At compile time, more
66involved type analysis can be performed. Therefore, the generated C++ code
67is generally more efficient than the result of the JIT compilation.
68
69There are limitations on what JavaScript constructs can be compiled to C++.
70For more information, see \l {Limitations when compiling JavaScript to C++}.
71
72\e{qmlsc} will be used instead of \e{qmlcachegen} if the Qt Quick Compiler
73Extensions are installed. It has the following additional features over
74\e{qmlcachegen}:
75
76\list
77 \li It can compile documents in Direct Mode. In that case, the C++ headers
78 of the types underpinning other QML components are directly included
79 and the methods of those types are directly called. Conversely, in
80 Indirect Mode, \e qmlcachegen or \e qmlsc call methods through the
81 lookup mechanism which is also used in the interpreter and JIT.
82 \li It can compile documents in Static Mode. In that case, \e qmlsc assumes
83 that no properties of any types exposed to C++ can be shadowed by
84 derived types. This allows for more bindings and functions to be
85 compiled, but generates invalid code if any properties are shadowed.
86\endlist
87
88Instead of producing C++ as output, \l {qmlsc} and \l {qmlcachegen} can also
89generate .qmlc, .jsc and .mjsc "cache files". These still contain a QML
90compilation unit each, and can be loaded by the QML engine to avoid
91re-compilation. They can only contain document structure and byte code, though.
92Compilation of bindings and functions to C++ is omitted if cache files are
93produced. Neither the CMake nor the qmake build system offered by Qt expose this
94functionality.
95
96\section1 Summary
97
98The following table summarizes the differences between \l{qmltc},
99\l{qmlcachegen} and \l{qmlsc}:
100
101\table
102 \header
103 \li \e qmltc
104 \li \e qmlcachegen
105 \li \e qmlsc
106 \row
107 \li Compiles QML types to C++ classes
108 \li Compiles QML documents to QML compilation units
109 \li Compiles QML documents to QML compilation units
110 \row
111 \li Generated output acts as faster alternative to
112 \l{QQmlComponent}-based object creation.
113 \li Generated output is used internally by the QML engine to avoid
114 re-compilation, and to speed up execution.
115 \li Generated output is used internally by the QML engine to avoid
116 re-compilation, and to speed up execution. Direct Mode and Static
117 Mode can further accelerate your application.
118 \row
119 \li Available for all versions of Qt
120 \li Available for all versions of Qt
121 \li Available for commercial customers
122\endtable
123*/