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
topic.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtqml-modules-topic.html
6\title QML Modules
7\brief Description of how to write modules for QML
8
9A QML module provides versioned types and JavaScript resources in a type
10namespace which may be used by clients who import the module. The types which
11a module provides may be defined in C++ within a plugin, or in QML documents.
12Modules make use of the QML versioning system which allows modules to be
13independently updated.
14
15Defining of a QML module allows:
16\list
17\li The sharing of common QML types within a project - for example, a group of
18 UI components that are used by different windows
19\li The distribution of QML-based libraries
20\li The modularization of distinct features, so that applications only load the
21 libraries necessary for their individual needs
22\li Versioning of types and resources so that the module can be updated safely
23 without breaking client code
24\endlist
25
26
27\section1 Defining a QML Module
28
29\note Use the \l{Port QML modules to CMake}{CMake API} to define a QML Module.
30Define your QML module manually only if you need to use \c qmake.
31
32A module is defined by a \l{qtqml-modules-qmldir.html}
33{module definition qmldir file}. Each module has an associated type
34namespace, which is the module's identifier. A module can provide QML object
35types (defined either by QML documents or via a C++ plugin) and JavaScript
36resources, and may be imported by clients.
37
38To define a module, a developer should gather together the various QML
39documents, JavaScript resources and C++ plugins which belong in the module
40into a single directory, and write an appropriate \l{qtqml-modules-qmldir.html}
41{module definition qmldir file} which should also be placed into the directory.
42The directory can then be installed into the
43\l{qtqml-syntax-imports.html#qml-import-path}{QML import path} as a module.
44
45Note that defining a module is not the only way to share common QML types
46within a project - a simple \l{Importing QML Document Directories}
47{QML document directory import} may also be used for this purpose.
48
49\section1 Supported QML Module Types
50
51There are two different types of modules supported by QML:
52\list
53\li \l{Identified Modules}
54\li \l{Legacy Modules} (deprecated)
55\endlist
56
57Identified modules explicitly define their identifier and are installed into
58QML import path. Identified modules are more maintainable (due to type
59versioning) and are provided with type registration guarantees by the QML
60engine which are not provided to legacy modules. Legacy modules are only
61supported to allow legacy code to continue to work with the latest version of
62QML, and should be avoided by clients if possible.
63
64Clients may import a QML module from within QML documents or JavaScript files.
65Please see the documentation about
66\l{qtqml-syntax-imports.html#module-namespace-imports}{importing a QML module}
67for more information on the topic.
68
69\section1 Providing Types and Functionality in a C++ Plugin
70
71An application which has a lot of logic implemented in C++, or which defines
72types in C++ and exposes them to QML, may wish to implement a QML plugin. A
73QML extension module developer may wish to implement some types in a C++ plugin
74(as opposed to defining them via QML documents) to achieve better performance
75or for greater flexibility.
76
77Every C++ plugin for QML has an initialiatization function which is called by
78the QML engine when it loads the plugin. This initialization function must
79register any types that the plugin provides, but must not do anything else
80(for example, instantiating QObjects is not allowed).
81
82See \l{qtqml-modules-cppplugins.html}{Creating C++ Plugins For QML} for more information.
83
84\sa {Modern QML modules}, {Port QML modules to CMake}
85*/