Doc: Cleanup of the Deploying QML Applications topic

Change-Id: Ie2fb81c48cab828c2d0706761478b7388ef6f630
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Kavindra Palaraja 2019-07-03 14:21:26 +02:00
parent c52d9c0696
commit 967e916d21
1 changed files with 110 additions and 130 deletions

View File

@ -28,138 +28,117 @@
/*! /*!
\page qtquick-deployment.html \page qtquick-deployment.html
\title Deploying QML Applications \title Deploying QML Applications
\brief Deploying QML applications \brief Provides information on how to use deploy QML applications.
QML documents are loaded and run by the QML runtime. This includes the Declarative UI engine
along with the built-in QML types and plugin modules. The QML runtime also provides access
to third-party QML types and modules.
QML documents are loaded and executed by the QML runtime. This includes the Applications that use QML must invoke the QML runtime to run QML documents. You can do this by
Declarative UI engine along with the built-in QML types and plugin modules, creating a QQuickView or a QQmlEngine, as described below. In addition, the Declarative UI
and it also provides access to third-party QML types and modules. package includes the qmlscene tool, which loads \c .qml files. This tool is useful for developing
and testing QML code without having to write a C++ application to load the QML runtime.
Applications that use QML need to invoke the QML runtime in order to
execute QML documents. This can be done by creating a QQuickView
or a QQmlEngine, as described below. In addition, the Declarative UI
package includes the qmlscene tool, which loads \c .qml files. This tool is
useful for developing and testing QML code without the need to write
a C++ application to load the QML runtime.
\section1 Deploying Applications with Qt Creator \section1 Deploying Applications with Qt Creator
\l{Qt Creator Manual}{Qt Creator} deploys and packages QML applications to \l{Qt Creator Manual}{Qt Creator} deploys and packages QML applications to various platforms.
various platforms. For mobile devices, Qt Creator can directly bundle For mobile devices, Qt Creator can directly bundle applications to the respective platform
applications to the respective platform package formats such as APK. package formats, such as APK.
For more information, visit: When you run your applications on the target platform, your application needs to access
\list the location of the QML libraries. If you use \l{qmake Manual}{qmake}, the
\li \l{Deploying Qt Applications} \c QT_INSTALL_QML environment variable points to the location of the libraries. The
\li \l{Qt Creator: Running on Multiple Platforms}{Running on Multiple Platforms} \l{Downloads}{Qt Installers} install the QML libraries in:
\li \l{Qt Creator: Deploying to Devices}{Deploying to Devices} \c{<version>}\c{/}\e{<compiler>}\c{/qml} directory.
\endlist
When running applications on the target platform, the application needs to
access the location of the QML libraries. When using \l{qmake Manual}{qmake},
the \c QT_INSTALL_QML environment points to the location of the libraries.
The \l{Downloads}{Qt Installers} install the QML libraries in
\e{<version>}\c{/}\e{<compiler>}\c{/qml} directory.
\section1 QML Caching \section1 QML Caching
The QML runtime loads QML documents by parsing them and generating byte code. The QML runtime loads QML documents by parsing them and generating byte code. Most of the time,
Most of the time the document hasn't changed since the last time it was loaded. the document hasn't changed since the last time it was loaded. To speed up this loading process,
In order to speed up this loading process, the QML runtime maintains a cache the QML runtime maintains a cache file for each QML document. This cache file contains the
file for each qml document. This cache file contains the compiled byte code and a compiled byte code and a binary representation of the QML document structure. In addition, when
binary representation of the QML document structure. In addition, when multiple multiple applications use the same QML document, the memory needed for the code is shared between
applications use the same QML document, the memory needed for the code is application processes. The cache files are loaded via the \c mmap() system call on POSIX-compliant
shared between application processes. The cache files are loaded via the \c operating systems or \c CreateFileMapping() on Windows, resulting in significant memory savings.
mmap() system call on POSIX compliant operating systems or \c
CreateFileMapping() on Windows, resulting in significant memory savings.
Each time you load a changed QML document, the cache is automatically Each time you load a changed QML document, the cache is automatically re-created. Cache files are
re-created. Cache files are located in a sub-directory of located in a sub-directory of QStandardPaths::CacheLocation with the name "qmlcache". The file
QStandardPaths::CacheLocation with the name "qmlcache". The file extension is \c extension is \c .qmlc for QML documents and \c .jsc for imported JavaScript modules.
.qmlc for QML documents and \c .jsc for imported JavaScript modules.
\section1 Compiling QML Ahead of Time \target Compiling Ahead of Time
\section1 Ahead-of-Time Compilation
The automatic caching of compiled QML documents into cache files results in The automatic caching of compiled QML documents into cache files results in significantly faster
significantly faster load times of applications. However, the initial creation application load time. However, the initial creation of cache files can still take time, especially
of cache files can still take time, especially when the application starts for when the application starts for the very first time. To avoid that initial step and provide faster
the very first time. To avoid that initial step and provide faster start-up startup times from the very beginning, Qt's build system allows you to perform the compilation step
times from the very beginning, Qt's build system allows you to perform the for QML files ahead of time, when compiling the C++ parts of your application.
compilation step for QML files at the same time as the rest of your
application.
If you would like to deploy your application with QML files compiled ahead of To deploy your application with QML files compiled ahead of time, you must organize the files and
time, then you must organize the files and the build system in a specific way: the build system in a specific way:
\list \list
\li All QML documents (including JavaScript files) must be included as resources \li All QML documents (including JavaScript files) must be included as resources via
through \l{The Qt Resource System}{Qt's Resource system}. \l{The Qt Resource System}{Qt's Resource system}.
\li Your application must load the QML documents via the \c qrc:/// URL scheme. \li Your application must load the QML documents via the \c qrc:/// URL scheme.
\li You can enable Ahead-of-Time compilation using the \c CONFIG+=qtquickcompiler directive. \li You can enable Ahead-of-Time compilation using the \c CONFIG+=qtquickcompiler directive.
\li If you are using the CMake build system, then you can achieve this inserting a \li If you're using the CMake build system, then you can achieve this by inserting a
\c find_package(Qt5QuickCompiler) call into your \c CMakeLists.txt and replace the use of \c find_package(Qt5QuickCompiler) call into your \c CMakeLists.txt and replacing the use
of \c qt5_add_resources with \c qtquick_compiler_add_resources. of \c qt5_add_resources with \c qtquick_compiler_add_resources.
\endlist \endlist
One added benefit of this way of developing and deploying the application is One benefit of compiling ahead of time is that, in the event of syntax errors in your QML
that you will be notified of syntax errors in your QML documents at application documents, you are notified at application compile-time instead of at run-time, when the file is
compile time, instead of run-time when loading the file. loaded.
If you have \c .qml or \c .js files which should not be compiled but just If you have \c .qml or \c .js files which should not be compiled but just bundled by the resource
bundled by the resource system (for example, \c .js files used with \l{Qt system, such as \c .js files used with \l{Qt WebEngine}, you can omit them from the compilation
WebEngine}), then you can omit them from the compilation by specifying the via the \c QTQUICK_COMPILER_SKIPPED_RESOURCES variable. In your project file, specify the resource
resources files that contain them in the \c QTQUICK_COMPILER_SKIPPED_RESOURCES files to omit, as follows:
variable in your project file, like below:
\code \code
QTQUICK_COMPILER_SKIPPED_RESOURCES += bundle_only.qrc QTQUICK_COMPILER_SKIPPED_RESOURCES += bundle_only.qrc
\endcode \endcode
By default this feature will tie your application to the Qt version you are By default, this feature ties your application to the Qt version you are compiling against, because
compiling against, because it will replace the QML document source code in the it replaces the QML document source code in the resources with the compiled binary version. The
resources with the compiled binary version. The source files are not present source files are not present anymore. Consequently, when you use the same application against a
anymore. That means that when using the same application against a different different version of Qt without recompiling it, loading the QML documents will fail with an error
version of Qt without recompiling it, loading the QML documents will fail with message.
an error message.
The Ahead-of-Time compilation is implemented this way because the feature Ahead-of-Time compilation is implemented this way because the feature originates from an add-on for
originates from an add-on for use in commercial application environments, where use in commercial application environments, where deploying source code is not desirable but it's
the deployment of source code is not desirable and requiring a recompilation usually acceptable to require a recompilation when changing Qt.
when changing Qt is usually acceptable.
You can retain the QML and JavaScript documents in the resources by passing a You can retain the QML and JavaScript documents in the resources by passing a list of resource
list of resource (*.qrc) files in the \c QTQUICK_COMPILER_RETAINED_RESOURCES \c{(*.qrc)} files in the \c QTQUICK_COMPILER_RETAINED_RESOURCES qmake variable. These resource
qmake variable. These resource files will then not be filtered, and any QML and files will then not be filtered, and any QML and JavaScript files specified in them will be
JavaScript files specified in them will be readable in full source by your readable in full source by your application. Then, if you run the application with a different
application. If you then run the application with a different version of Qt, Qt version, the QML and JavaScript files will be recompiled at runtime, slowing down the
the QML and JavaScript files will be recompiled at runtime, slowing down the
initial start of the application. initial start of the application.
\section1 Prototyping with QML Scene \section1 Prototyping with QML Scene
The Declarative UI package includes a QML runtime tool, The Declarative UI package includes a QML runtime tool, \l{qtquick-qmlscene.html}{qmlscene},
\l{qtquick-qmlscene.html}{qmlscene}, which loads and displays QML documents. which loads and displays QML documents. This is useful during the application development phase
This is useful during the application development phase for prototyping for prototyping QML-based applications without writing your own C++ applications to invoke
QML-based applications without writing your own C++ applications to invoke
the QML runtime. the QML runtime.
\section1 Initializing the QML Runtime in Applications \section1 Initializing the QML Runtime in Applications
To run an application that uses QML, the QML runtime must be invoked by To run an application that uses QML, your application must invoke the QML runtime. This is done
the application. This is done by writing a Qt C++ application that loads the by writing a Qt C++ application that loads the QQmlEngine by either:
QQmlEngine by either:
\list \list
\li Loading the QML file through a QQuickView instance, or \li Loading the QML file through a QQuickView instance.
\li Creating a QQmlEngine instance and loading QML files with QQmlComponent \li Creating a QQmlEngine instance and loading QML files with QQmlComponent.
\endlist \endlist
\section2 Initializing with QQuickView \section2 Initializing with QQuickView
QQuickView is a QWindow-based class that is able to load QML files. QQuickView is a QWindow-based class that can load QML files. For example, if there is a QML file,
For example, if there is a QML file, \c application.qml, it will \c application.qml, it will look like this:
look like this:
\qml \qml
import QtQuick 2.3 import QtQuick 2.3
@ -185,8 +164,7 @@ It can be loaded in a Qt application's \c main.cpp file like this:
} }
\endcode \endcode
This creates a QWindow-based view that displays the contents of This creates a QWindow-based view that displays the contents of \c application.qml.
\c application.qml.
The application's \c .pro \l{Creating Project Files}{project file} must specify The application's \c .pro \l{Creating Project Files}{project file} must specify
the \c declarative module for the \c QT variable. For example: the \c declarative module for the \c QT variable. For example:
@ -198,12 +176,12 @@ the \c declarative module for the \c QT variable. For example:
\endcode \endcode
\section2 Creating a QQmlEngine directly \section2 Creating a QQmlEngine Directly
If \c application.qml does not have any graphical components, or if it is If \c application.qml doesn't have any graphical components, or if it's preferred to
preferred to avoid QQuickView for other reasons, the QQmlEngine avoid QQuickView for other reasons, the QQmlEngine can be constructed directly instead.
can be constructed directly instead. In this case, \c application.qml is In this case, \c application.qml is loaded as a QQmlComponent instance rather than placed into
loaded as a QQmlComponent instance rather than placed into a view: a view:
\code \code
#include <QGuiApplication> #include <QGuiApplication>
@ -227,41 +205,36 @@ loaded as a QQmlComponent instance rather than placed into a view:
} }
\endcode \endcode
QGuiApplication can be replaced by a QCoreApplication in the code above in case you are not If you're not using any graphical items from Qt Quick, you can replace QGuiApplication with a
using any graphical items from Qt Quick. This allows using QML as a language without any QCoreApplication in the code above. This way, you can use QML as a language without any
dependencies to the \l{Qt GUI} module. dependencies to the \l{Qt GUI} module.
See \l{qtqml-cppintegration-data.html}{qtqml-cppintegration-exposecppattributes.html}{Exposing Attributes of C++ Types to QML}
for more information about using QQmlEngine, QQmlContext and QQmlComponent, as well
as details on including QML files through \l{The Qt Resource System}{Qt's Resource system}.
\section1 Managing Resource Files with the Qt Resource System \section1 Managing Resource Files with the Qt Resource System
The \l {The Qt Resource System}{Qt resource system} allows resource files to be stored as The \l {The Qt Resource System}{Qt resource system} allows resource files to be stored as binary
binary files in an application executable. This can be useful when building a mixed files in an application executable. This can be useful when building a mixed QML/C++ application
QML/C++ application as it enables QML files (as well as other resources such as images as it enables QML files and other resources -- such as images and sound files -- to be referred
and sound files) to be referred to through the resource system URI scheme rather than to through the resource system URI scheme rather than relative or absolute paths to filesystem
relative or absolute paths to filesystem resources. Note, however, that if you use the resource resources.
system, the application executable must be re-compiled whenever a QML source file is changed
in order to update the resources in the package. \note If you use the resource system, the application executable must be re-compiled whenever a
QML source file is changed, to update the resources in the package.
To use the resource system in a mixed QML/C++ application: To use the resource system in a mixed QML/C++ application:
\list \list
\li Create a \c .qrc \l {The Qt Resource System}{resource collection file} that lists resource \li Create a \c .qrc \l {The Qt Resource System}{resource collection file} that lists resource
files in XML format files in XML format.
\li From C++, load the main QML file as a resource using the \c :/ prefix or as a URL with the \li From C++, load the main QML file as a resource using the \c :/ prefix or as a URL with the
\c qrc scheme \c .qrc scheme.
\endlist \endlist
Once this is done, all files specified by relative paths in QML will be loaded from Once this is done, all files specified by relative paths in QML are loaded from the resource
the resource system instead. Use of the resource system is completely transparent to system instead. Use of the resource system is completely transparent to the QML layer; this means
the QML layer; this means all QML code should refer to resource files using relative all QML code should refer to resource files using relative paths and should \b not use the \c .qrc
paths and should \e not use the \c qrc scheme. This scheme should only be used from scheme. This scheme should only be used from C++ code to refer to resource files.
C++ code for referring to resource files.
Here is a application packaged using the \l {The Qt Resource System}{Qt resource system}. Here's an application packaged using the Qt resource system; its directory structure is as follows:
The directory structure looks like this:
\code \code
project project
@ -273,26 +246,33 @@ project
|- project.pro |- project.pro
\endcode \endcode
The \c main.qml and \c background.png files will be packaged as resource files. This is The \c main.qml and \c background.png files are packaged as resource files. This is done in the
done in the \c example.qrc resource collection file: \c example.qrc resource collection file:
\quotefile qmlapp/qtbinding/resources/example.qrc \quotefile qmlapp/qtbinding/resources/example.qrc
Since \c background.png is a resource file, \c main.qml can refer to it using the relative Since \c background.png is a resource file, \c main.qml can refer to it using the relative path
path specified in \c example.qrc: specified in \c example.qrc:
\snippet qmlapp/qtbinding/resources/main.qml 0 \snippet qmlapp/qtbinding/resources/main.qml 0
To allow QML to locate resource files correctly, the \c main.cpp loads the main QML To allow QML to locate resource files correctly, the \c main.cpp loads the main QML file,
file, \c main.qml, as a resource file using the \c qrc scheme: \c main.qml, as a resource file using the \c .qrc scheme:
\snippet qmlapp/qtbinding/resources/main.cpp 0 \snippet qmlapp/qtbinding/resources/main.cpp 0
Finally, \c project.pro uses the RESOURCES variable to indicate that \c example.qrc should Finally, \c project.pro uses the \c RESOURCES variable to indicate that \c example.qrc should
be used to build the application resources: be used to build the application resources:
\quotefile qmlapp/qtbinding/resources/resources.pro \quotefile qmlapp/qtbinding/resources/resources.pro
See \l {The Qt Resource System} for more information. \section1 Related Information
\list
\li \l{Deploying Qt Applications}
\li \l{Qt Creator: Running on Multiple Platforms}{Running on Multiple Platforms}
\li \l{Qt Creator: Deploying to Devices}{Deploying to Devices}
\li \l{qtqml-cppintegration-data.html}{qtqml-cppintegration-exposecppattributes.html}{Exposing Attributes of C++ Types to QML}
\li \l{The Qt Resource System}
\endlist
*/ */