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
qt6-modernize-qml-modules.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2025 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qt6-modernize-qml-modules.html
6
\title Modern QML modules
7
\brief Modernize your QML modules.
8
9
QML modules have become more powerful and easier to use in Qt 6.
10
The following sections describe how to modernize QML modules that already use \l qt_add_qml_module.
11
12
See also \l{Port QML modules to CMake} on how to port a QML module to the
13
\l qt_add_qml_module CMake API.
14
15
\section1 Use qt_standard_project_setup
16
\l qt_standard_project_setup sets up \l{Qt CMake policies} needed for modern QML modules, among
17
other things. To modernize your QML module and follow best practices, call
18
\l qt_standard_project_setup in the project's top-level \c CMakeLists.txt file before any
19
\l qt_add_qml_module call:
20
\badcode
21
qt_standard_project_setup(REQUIRES 6.8)
22
\endcode
23
24
\section1 Use the new standard resource path prefix
25
The standard resource path for QML modules moved from \c{:/} to \c{:/qt/qml} with \l{QTP0001}. Don't
26
use custom resource prefixes nor extend import paths in the engine.
27
Remove all \c RESOURCE_PREFIX arguments from all \l qt_add_qml_module calls, as well as all calls to
28
\l{QQmlEngine::addImportPath} or similar.
29
Change all qrc paths in your C++ and QML code to use the new resource path prefix:
30
\badcode
31
// C++ usages like:
32
QUrl someUrl("qrc:/MyQmlModule/MyResource1.png");
33
// need to be changed to
34
QUrl someUrl("qrc:/qt/qml/MyQmlModule/MyResource1.png");
35
36
// QML usages like:
37
":/MyQmlModule/MyResource1.png"
38
// need to be changed to
39
":/qt/qml/MyQmlModule/MyResource1.png"
40
\endcode
41
42
See also \l{Using the Qt Resource System with QML}.
43
44
\section1 Use loadFromModule to load your QML files
45
46
With the default import path, you can use the \c loadFromModule methods, like \l
47
QQmlApplicationEngine::loadFromModule, \l QQuickView::loadFromModule, or
48
\l QQmlComponent::loadFromModule, for example.
49
50
Use \c loadFromModule to load your QML file, for example:
51
\badcode
52
engine.load(QUrl(QStringLiteral("qrc:/MyQmlModule/Main.qml")));
53
// becomes
54
engine.loadFromModule("MyQmlModule", "Main");
55
\endcode
56
57
\section1 Replace OUTPUT_DIRECTORY and IMPORT_PATH with DEPENDENCIES TARGET
58
Avoid setting an \c IMPORT_PATH in the \l qt_add_qml_module. Instead, use \c{DEPENDENCIES TARGET}
59
to declare dependencies to other QML modules that can't be found in the current import path.
60
61
Using \c{DEPENDENCIES TARGET} also eliminates the need for the \c QT_QML_OUTPUT_DIRECTORY CMake
62
variable and the \c OUTPUT_DIRECTORY argument to \l qt_add_qml_module, so remove their
63
definitions and usages.
64
65
For example:
66
\badcode
67
### in the CMakeLists.txt file defining the dependent QML module:
68
# don't set QT_QML_OUTPUT_DIRECTORY and remove lines like these:
69
set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/qml)
70
71
qt_add_qml_module(MyThirdPartyQmlLibraryDependency
72
URI MyThirdPartyQmlLibraryDependency
73
....
74
# custom output paths are obsolete due to DEPENDENCIES TARGET below, so remove:
75
OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/qml
76
}
77
78
### in the CMakeLists.txt file defining the QML module that uses the dependency:
79
qt_add_qml_module(MyQmlLibrary
80
URI MyQmlModule
81
...
82
# replace import paths like these:
83
IMPORT_PATH ${CMAKE_CURRENT_BINARY_DIR}/thirdparty/qml
84
# with:
85
DEPENDENCIES TARGET MyThirdPartyQmlLibraryDependency
86
}
87
\endcode
88
89
\note You might need to call \c{add_subdirectory()} before calling \l qt_add_qml_module in your
90
CMakeLists.txt for \c {DEPENDENCIES TARGET} to find the target.
91
92
For more information on how to declare module dependencies, see \l{Declaring module dependencies}.
93
94
\sa {Changes to Qt QML}, {Port QML modules to CMake}
95
*/
qtdeclarative
src
qml
doc
src
qt6-modernize-qml-modules.qdoc
Generated on
for Qt by
1.14.0