- Qt 6.11
- Qt Qml
- Qt Qml Tooling
- Qt Quick Compiler
- Qt 6.11.2 Reference Documentation

Qt Qml Tooling ◦ QML script compiler

# Qt Quick Compiler

Qt Quick Compiler lets you process QML and JavaScript code at compile time, rather than at run time. This allows for:

- Faster application startup
- Faster evaluation of bindings and functions

The Qt Quick Compiler consist of two components:

- QML type compiler
- QML script compiler

Note: qmltc, qmlsc and qmlcachegen are internal build tools. If you need to care about their invocation, you are either writing a build system, or you are doing something wrong.

## The QML type compiler

The QML type compiler, (qmltc) compiles QML types to C++ classes. These C++ classes are then added to your application and can be instantiated from other C++ code. This way you can avoid much of the overhead of using QQmlComponent to create instances of your QML types. In order to benefit from qmltc, you need to adapt your C++ code and make use of the new classes.

qmltc can only compile a QML document if it completely understands its structure. It will fail if an unsupported language feature is encountered. It does not have to understand the JavaScript code in bindings and functions, though.

## The QML script compiler

The QML script compiler, (qmlsc and qmlcachegen) compiles bindings and functions to both, an efficient byte code and C++ functions. This process automatically happens behind the scenes if you are using qt_add_qml_module to specify your QML modules. For more information about available options to control different aspects of QML compilation, see Caching compiled QML sources.

At compile time, for each QML or JavaScript document a corresponding C++ file is created and built into the application. The C++ file then contains a QML compilation unit, which consists of:

- An efficient representation of the document structure
- Byte code for all functions and bindings in the document
- C++ code for functions and bindings the compiler fully understands

The QML engine then refrains from compiling the QML or JavaScript source code at run time and instead uses the pre-built compilation unit to load the QML component and its functions and bindings more quickly. The functions and bindings that were compiled to C++ can also be executed faster. Other bindings and functions are either interpreted directly from the byte code, or compiled to machine code via a JIT compilation step at run time. At compile time, more involved type analysis can be performed. Therefore, the generated C++ code is generally more efficient than the result of the JIT compilation.

There are limitations on what JavaScript constructs can be compiled to C++. For more information on these limitations, see Limitations when compiling JavaScript to C++. For statistics on how well a QML project can be compiled, see Obtaining statistics about the compilation of functions and bindings.

qmlsc will be used instead of qmlcachegen if the Qt Quick Compiler Extensions are installed. It has the following additional features over qmlcachegen:

- It can compile documents in Direct Mode. In that case, the C++ headers of the types underpinning other QML components are directly included and the methods of those types are directly called. Conversely, in Indirect Mode, qmlcachegen or qmlsc call methods through the lookup mechanism which is also used in the interpreter and JIT.
- It can compile documents in Static Mode. In that case, qmlsc assumes that no properties of any types exposed to C++ can be shadowed by derived types. This allows for more bindings and functions to be compiled, but generates invalid code if any properties are shadowed.

Instead of producing C++ as output, qmlsc and qmlcachegen can also generate .qmlc, .jsc and .mjsc "cache files". These still contain a QML compilation unit each, and can be loaded by the QML engine to avoid re-compilation. They can only contain document structure and byte code, though. Compilation of bindings and functions to C++ is omitted if cache files are produced. Neither the CMake nor the qmake build system offered by Qt expose this functionality.

## Summary

The following table summarizes the differences between qmltc, qmlcachegen and qmlsc:

qmltc | qmlcachegen | qmlsc
--- | --- | ---
Compiles QML types to C++ classes | Compiles QML documents to QML compilation units | Compiles QML documents to QML compilation units
Generated output acts as faster alternative to QQmlComponent-based object creation. | Generated output is used internally by the QML engine to avoid re-compilation, and to speed up execution. | Generated output is used internally by the QML engine to avoid re-compilation, and to speed up execution. Direct Mode and Static Mode can further accelerate your application.
Available for all versions of Qt | Available for all versions of Qt | Available for commercial customers

Qt Qml Tooling ◦ QML script compiler