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
deployment.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 qtquick-deployment.html
6\title Deploying QML Applications
7\brief Provides information on how to use deploy QML applications.
8
9QML documents are loaded and run by the QML runtime. This includes the
10Declarative UI engine along with the built-in QML types and plugin modules. The
11QML runtime also provides access to third-party QML types and modules.
12
13Applications that use QML must invoke the QML runtime to run QML documents. You
14can do this by creating a QQuickView or a QQmlEngine, as described below. In
15addition, the Declarative UI package includes the \c qml tool, which loads
16\c {.qml} files. This tool is useful for developing and testing QML code without
17having to write a C++ application to load the QML runtime.
18
19\section1 Deploying applications with \QC
20
21\l{\QC Documentation}{\QC} deploys and packages QML applications to various
22platforms. For mobile devices, \QC can directly bundle applications to the
23respective platform package formats, such as APK.
24
25When you run your applications on the target platform, your application needs
26to access the location of the QML libraries. If you use
27\l{qmake Manual}{qmake}, the \c QT_INSTALL_QML environment variable points to
28the location of the libraries. The \l{Downloads}{Qt Installers} install the QML
29libraries in: \c{<version>}\c{/}\e{<compiler>}\c{/qml} directory.
30
31\section1 QML Caching
32
33The QML runtime loads QML documents by parsing them and generating byte code.
34Most of the time, the document hasn't changed since the last time it was
35loaded. To speed up this loading process, the QML runtime maintains a cache
36file for each QML document. This cache file contains the compiled byte code and
37a binary representation of the QML document structure. In addition, when
38multiple applications use the same QML document, the memory needed for the code
39is shared between application processes. The cache files are loaded via the
40\c mmap() system call on POSIX-compliant operating systems or
41\c CreateFileMapping() on Windows, resulting in significant memory savings.
42
43Each time you load a changed QML document, the cache is automatically
44re-created. Cache files are located in a sub-directory of
45QStandardPaths::CacheLocation with the name "qmlcache". The file extension is
46\c .qmlc for QML documents and \c .jsc for imported JavaScript modules.
47
48\target Compiling Ahead of Time
49\section1 Ahead-of-Time compilation
50
51The automatic caching of compiled QML documents into cache files results in
52significantly faster application load time. However, the initial creation of
53cache files can still take time, especially when the application starts for the
54very first time. To avoid that initial step and provide faster startup times
55from the very beginning, Qt's build system allows you to perform the
56compilation step for QML files ahead of time, when compiling the C++ parts of
57your application.
58
59One benefit of compiling ahead of time is that, in the event of syntax errors
60in your QML documents, you are notified at application compile-time instead of
61at run-time, when the file is loaded.
62
63This will happen automatically if you use the
64\l{qt_add_qml_module}{CMake QML Module API}, for qmake see the section below.
65
66\section2 qmake
67
68When using qmake, in order to deploy your application with QML files compiled
69ahead of time, you must organize the files and the build system in a specific
70way:
71
72\list
73 \li All QML documents (including JavaScript files) must be included as
74 resources via \l{The Qt Resource System}{Qt's Resource system}.
75 \li Your application must load the QML documents via the \c qrc:/// URL
76 scheme.
77 \li You can enable Ahead-of-Time compilation using the
78 \c CONFIG+=qtquickcompiler directive.
79\endlist
80
81\section1 Prototyping with QML Scene
82
83The Declarative UI package includes a QML Runtime Tool,
84\l{qml_runtime_tool}{qml}, which loads and displays QML documents. This is
85useful during the application development phase for prototyping QML-based
86applications without writing your own C++ applications to invoke the QML
87runtime.
88
89\section1 Initializing the QML Runtime in Applications
90
91To run an application that uses QML, your application must invoke the QML
92runtime. This is done by writing a Qt C++ application that loads the QQmlEngine
93by either:
94
95\list
96 \li Loading the QML file through a QQuickView instance.
97 \li Creating a QQmlEngine instance and loading QML files with QQmlComponent.
98\endlist
99
100
101\section2 Initializing with QQuickView
102
103QQuickView is a QWindow-based class that can load QML files. For example, if
104there is a QML file, \c application.qml, it will look like this:
105
106\qml
107 import QtQuick
108
109 Rectangle { width: 100; height: 100; color: "red" }
110\endqml
111
112It can be loaded in a Qt application's \c main.cpp file like this:
113
114\code
115 #include <QGuiApplication>
116 #include <QQuickView>
117
118 int main(int argc, char *argv[])
119 {
120 QGuiApplication app(argc, argv);
121
122 QQuickView view;
123 view.setSource(QUrl::fromLocalFile("application.qml"));
124 view.show();
125
126 return app.exec();
127 }
128\endcode
129
130This creates a QWindow-based view that displays the contents of
131\c {application.qml}.
132
133\section3 Build files
134
135\if defined(onlinedocs)
136 \tab {build-qt-app}{tab-cmake}{CMake}{checked}
137 \tab {build-qt-app}{tab-qmake}{qmake}{}
138 \tabcontent {tab-cmake}
139\else
140 \section1 Using CMake
141\endif
142 \include {module-use.qdocinc} {building with cmake} {Quick}
143\if defined(onlinedocs)
144 \endtabcontent
145 \tabcontent {tab-qmake}
146\else
147 \section1 Using qmake
148\endif
149 \include {module-use.qdocinc} {building_with_qmake} {quick}
150 For more information, see \l{Creating Project Files}.
151\if defined(onlinedocs)
152 \endtabcontent
153\endif
154
155\section2 Creating a QQmlEngine directly
156
157If \c application.qml doesn't have any graphical components, or if it's
158preferred to avoid QQuickView for other reasons, the QQmlEngine can be
159constructed directly instead. In this case, \c application.qml is loaded as a
160QQmlComponent instance rather than placed into a view:
161
162\code
163 #include <QGuiApplication>
164 #include <QQmlEngine>
165 #include <QQmlContext>
166 #include <QQmlComponent>
167
168 int main(int argc, char *argv[])
169 {
170 QGuiApplication app(argc, argv);
171
172 QQmlEngine engine;
173 QQmlContext *objectContext = new QQmlContext(engine.rootContext());
174
175 QQmlComponent component(&engine, "application.qml");
176 QObject *object = component.create(objectContext);
177
178 // ... delete object and objectContext when necessary
179
180 return app.exec();
181 }
182\endcode
183
184If you're not using any graphical items from Qt Quick, you can replace
185QGuiApplication with a QCoreApplication in the code above. This way, you can
186use QML as a language without any dependencies to the \l{Qt GUI} module.
187
188\section1 Using the Qt Resource System with QML
189
190The \l {The Qt Resource System}{Qt resource system} allows resource files to be
191stored as binary files in an application executable. The Qt Resource System is
192used for QML application as it enables QML files and other resources -- such as
193images and sound files -- to be referred to through the resource system URI
194scheme rather than relative or absolute paths to filesystem resources.
195
196\note Usage of the resource system means that the application executable
197usually must be re-compiled whenever a QML source file is changed, to update
198the resources in the package.
199
200The \l{qt_add_qml_module}{CMake QML Module API} automatically places your QML
201files in the resource system. To access them, load your main QML file as a
202resource or as a URL with the \c{qrc} scheme. The path in the resource system
203where your QML files are placed can be found by concatenating:
204
205\list
206\li the \c RESOURCE_PREFIX you have passed to \l{qt_add_qml_module}.
207\li \c{/qt/qml}, if you have \e{not} passed \c RESOURCE_PREFIX to
208 \l{qt_add_qml_module} and \l{QTP0001} policy is set to \c NEW.
209\li \c{/}, if you have \e{not} passed \c RESOURCE_PREFIX to \l{qt_add_qml_module}
210 and \l{QTP0001} policy is \c{not} set to \c NEW.
211\li If you have \e{not} passed \c NO_RESOURCE_TARGET_PATH to \l{qt_add_qml_module}:
212 the \c URI you have passed to \l{qt_add_qml_module} with dots replaced by slashes.
213\endlist
214
215For example, a module called \c{My.Own.Module} is placed at:
216\list
217\li \c{:/qt/qml/My/Own/Module/} if you have specified \c{/qt/qml} as \c RESOURCE_PREFIX, or
218 you have \e{not} passed \c RESOURCE_PREFIX and \l{QTP0001} policy is set to \c NEW.
219\li \c{:/My/Own/Module/} if you have specified \c{/} as \c RESOURCE_PREFIX, or
220 you have \e{not} passed \c RESOURCE_PREFIX and \l{QTP0001} policy is \e{not} set to \c NEW.
221}
222\li \c{:/Some/Prefix/My/Own/Module/} if you have specified \c{Some/Prefix/} as \c RESOURCE_PREFIX
223\li \c{:/} if you have specified \c NO_RESOURCE_TARGET_PATH
224\endlist
225
226Once this is done, all files specified by relative paths in QML are loaded from
227the resource system. Use of the resource system is completely transparent to
228the QML layer; this means all QML code should refer to resource files using
229relative paths and should \e{not} use the \c{qrc} scheme. This scheme should
230only be used from C++ code to refer to resource files.
231
232\note When using qmake, you need to manually place your files in the resource
233system. See the \l{qmake Manual} and the \l{The Qt Resource System}{general
234documentation} on the resouce system for how to do this. It's advisable to
235still follow the path naming convention outlined above.
236
237\section1 Related Information
238\list
239 \li \l{Deploying Qt Applications}
240 \li \l{\QC: Run on many platforms}
241 \li \l{Data Type Conversion Between QML and C++}{Exposing Attributes of C++ Types to QML}
242 \li \l{The Qt Resource System}
243\endlist
244
245*/