Tying it all together with CMake | Qt Qml | Qt 6.11.0
Description: Building QML modules with CMake

- Qt 6.11
- Qt Qml
- The QML Reference
- QML Modules
- Tying it all together with CMake
- Qt 6.11.0 Reference Documentation

Exposing State from C++ to QML ◦ Modernizing QML modules

# Tying it all together with CMake

When creating QML modules, CMake provides the infrastructure to properly register QML types, generate necessary metadata, and ensure your module is correctly packaged and deployable. This page outlines the recommended workflow.

## Using qt_add_qml_module

The qt_add_qml_module command is the standard and recommended way to create QML modules. It handles all the complex details of QML module creation:

     qt_add_qml_module(my_qml_module
         URI MyModule
         QML_FILES
             MyType.qml
             AnotherType.qml
         SOURCES
             mytype.cpp mytype.h
     )

This single command:

- Creates the module target
- Registers QML types from C++ and QML files
- Generates a qmldir file
- Handles type registration
- Sets up proper import paths
- Enables QML tooling support (qmllint, qmlls, etc.)

## Adding Further QML Files

For QML files added after the initial `qt_add_qml_module` call, use qt_target_qml_sources:

     qt_target_qml_sources(my_qml_module
         QML_FILES
             DynamicallyAddedType.qml
     )

This can be done based on platform, configuration or other factors.

## Detailed CMake Reference

For complete details on all CMake commands, properties, variables, and policies, see CMake Integration for QML.

See also QML Modules and qt_add_qml_module.

Exposing State from C++ to QML ◦ Modernizing QML modules