C

qul_add_qml_module

Synopsis

qul_add_qml_module(<target>
                   URI <uri>
                   [QML_FILES <file paths...>]
                   [HEADERS <file paths...>]
                   [SOURCES <file paths...>]
                   [IMPORTS <uris...>]
                   [OUTPUT_DIRECTORY <path>]
                   [GENERATE_QMLTYPES]
                   [INSTALL_HEADERS_LOCATION <path>]
                   [INSTALL_HEADERS_COMPONENT <name>]
                   [INSTALL_LIBRARY_LOCATION <path>]
                   [INSTALL_LIBRARY_COMPONENT <name>]
                   [INSTALL_LIBRARY_EXPORT <name>]
)

Description

Creates a CMake target for building a QML module static library with the given URI.

A QML module directory is produced in a subdirectory of the current build directory (see OUTPUT_DIRECTORY).

The target is set up such that application targets which link it can import the QML module.

Optionally, module installation can be set up with the INSTALL_ arguments.

Options

The URI argument is a dot-separated QML module URI, which must be imported in QML to use the module. For example, the URI "Ui.Buttons" would lead to a module to be imported as "import Ui.Buttons 1.0".

The QML_FILES, HEADERS, and SOURCES arguments list the files that are part of the module. They are convenience wrappers for other CMake commands:

ArgumentFile TypeEquivalent Command
QML_FILESQML sourcesqul_target_qml_sources
HEADERSC++ headers (may have exported types)qul_target_generate_interfaces
SOURCESC++ source filestarget_sources

The IMPORTS argument can contain a list of QML module URIs that are dependencies of the new module, for example "IMPORTS QtQuick QtQuick.Templates".

The OUTPUT_DIRECTORY argument can override the subdirectory under which the QML module is built. The default is CMAKE_CURRENT_BINARY_DIR. The module files are placed into a subdirectory based on the import URI. For example, the subdirectory for QtQuick.Timeline would be QtQuick/Timeline under the OUTPUT_DIRECTORY.

The QML module files are placed in a subdirectory of OUTPUT_DIRECTORY which is generated by taking the URI and replacing all "." with "/".

When the GENERATE_QMLTYPES argument is used, a "plugins.qmltypes" file is generated for the module and placed next to the generated "qmldir" file.

Installation

The following files from OUTPUT_DIRECTORY are needed by users of the module:

  • The generated "qmldir" file
  • Source .qml files (listed in QML_FILES)
  • Generated .h files (from files in QML_FILES)
  • Generated .fonts files (from files in QML_FILES)
  • Generated .qml files (from headers in HEADERS)

Additionally, users of the module need access to the headers listed in HEADERS. They are never set up for installation by this command.

If the INSTALL_HEADERS_LOCATION argument is present, the required files from OUTPUT_DIRECTORY are installed to the given path. Note that the subdirectory for the module URI will be appended to the path automatically.

The optional INSTALL_HEADERS_COMPONENT argument controls the COMPONENT that is used for the CMake install(DIRECTORY) command.

If the INSTALL_LIBRARY_LOCATION argument is present, the compiled library is installed to the given location. The optional INSTALL_LIBRARY_COMPONENT and INSTALL_LIBRARY_EXPORT arguments specify the COMPONENT and EXPORT arguments that are passed to the CMake install(TARGETS) command.

Examples

qul_add_qml_module(simple_qml_only_module
    URI MySimpleModule
    QML_FILES
        Foo.qml
        Bar.qml
)

# myapp has QML files that "import MySimpleModule" and use Foo and Bar
target_link_libraries(myapp simple_qml_only_module)


qul_add_qml_module(myqmlmodule
    URI MyOrg.Styles
    QML_FILES
        Foo.qml
    HEADERS
        include/backend.h
    SOURCES
        source/backend.cpp
    IMPORTS
        QtQuick.Templates
    OUTPUT_DIRECTORY
        # Means that files will be ${CMAKE_CURRENT_BINARY_DIR}/imports/MyOrg/Styles/...
        ${CMAKE_CURRENT_BINARY_DIR}/imports/
    GENERATE_QMLTYPES
    INSTALL_HEADERS_LOCATION "${CMAKE_INSTALL_INCLUDEDIR}/qmlmodules"
    INSTALL_HEADERS_COMPONENT ModuleHeaders
    INSTALL_LIBRARY_LOCATION ${CMAKE_INSTALL_LIBDIR}
    INSTALL_LIBRARY_COMPONENT ObjectLibraries
    INSTALL_LIBRARY_EXPORT SharedExportName
)

target_include_directories(myqmlmodule
    PUBLIC
        "include/"
)
target_link_libraries(myqmlmodule PUBLIC
    Qul::ControlsTemplates
)

# Need to ensure that backend.h is installed and in the include path separately.
install(
    DIRECTORY "include/"
    DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/imports/include/
)
target_include_directories(myqmlmodule
    INTERFACE
        $<INSTALL_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/imports/include/>
)

See also Defining singletons in QML.

Available under certain Qt licenses.
Find out more.