Deploying QML Applications
QML documents are loaded and executed by the QML runtime. This includes the Declarative UI engine along with the built-in QML types and plugin modules, and it also provides access to third-party QML types and modules.
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 .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.
Deploying Applications with Qt Creator
Qt Creator deploys and packages QML applications to various platforms. For mobile devices, Qt Creator can directly bundle applications to the respective platform package formats such as APK and BAR.
For more information, visit:
When running applications on the target platform, the application needs to access the location of the QML libraries. When using qmake, the QT_INSTALL_QML
environment points to the location of the libraries. The Qt Installers install the QML libraries in <version>/
<compiler>/qml
directory.
QML Caching
The QML runtime loads QML documents by parsing them and generating native code. Most of the time the document hasn't changed since the last time it was loaded. In order to speed up this loading process, the QML runtime maintains a cache file for each qml document. This cache file contains the native code and a binary representation of the QML document structure. In addition, when multiple applications use the same QML document, the memory needed for the code is shared between application processes. The cache files are loaded via the mmap()
system call on POSIX compliant operating systems or CreateFileMapping()
on Windows, resulting in significant memory savings.
Each time you load a changed QML document, the cache is automatically re-created. Cache files are located in the same directory as the source code, if the directory is writable. Otherwise they will be placed in a sub-directory of QStandardPaths::CacheLocation with the name "qmlcache". The file extension is .qmlc
for QML documents and .jsc
for imported JavaScript modules.
QML Caching for Deployment (Preview)
The automatic caching of QML documents into cache files result in significantly faster load times of applications. However, the initial creation of cache files can still take time, especially when the application starts first. To avoid that initial step and provide faster start-up times from the very beginning, Qt's build system allows you to create these cache files in advance.
If you would like to deploy your application with cache files generated ahead of time, you must satisfy four conditions in your .pro
file:
- All QML documents (including JavaScript files) must be added to the
QML_FILES
variable. - Your .pro file must use the
load(qml_module)
orload(qml_plugin)
directive at the end, to activate the processing ofQML_FILES
and generation of install rules. - The
TARGETPATH
variable must contain the import name of your QML module with forward slashes as separators. - You must enable Ahead-of-Time caching using the
CONFIG+=qmlcache
directive.
For example if you are developing the module MyCompany.CommonComponents
, then your .pro
file could look like this:
TARGETPATH = MyCompany/CommonComponents QML_FILES = BlueButton.qml RedSlider.qml qmldir CONFIG += qmlcache load(qml_module)
Similarly, if your module contains a C++ plugin then you use qml_plugin
:
TARGETPATH = MyCompany/CommonComponents QML_FILES = BlueButton.qml RedSlider.qml qmldir CONFIG += qmlcache SOURCES = plugin.cpp QT += quick load(qml_plugin)
In these examples the QML module consisting of the QML documents, the qmldir
file, and optionally the C++ plugin, will be installed into the MyCompany/CommonComponents sub-directory of $$[QT_INSTALL_QML]
. By enabling the qmlcache
configuration, the cache files will be created at build time and also installed into the same directory for deployment.
Limitations
Currently this feature has some limitations:
- Only QML and JavaScript documents that are part of a QML module can be compiled ahead of time.
- For cross-compilation, only the ARMv7 and ARMv8 target architectures are supported.
- For native compilation, Ahead-of-Time caching is limited to architectures where the QML runtime supports Just-in-Time compilation. This includes x86, x86-64, ARMv7, ARMv8 and MIPS32.
Prototyping with QML Scene
The Declarative UI package includes a QML runtime tool, qmlscene, which loads and displays QML documents. This is useful during the application development phase for prototyping QML-based applications without writing your own C++ applications to invoke the QML runtime.
Initializing the QML Runtime in Applications
To run an application that uses QML, the QML runtime must be invoked by the application. This is done by writing a Qt C++ application that loads the QQmlEngine by either:
- Loading the QML file through a QQuickView instance, or
- Creating a QQmlEngine instance and loading QML files with QQmlComponent
Initializing with QQuickView
QQuickView is a QWindow-based class that is able to load QML files. For example, if there is a QML file, application.qml
, it will look like this:
import QtQuick 2.3 Rectangle { width: 100; height: 100; color: "red" }
It can be loaded in a Qt application's main.cpp
file like this:
#include <QGuiApplication> #include <QQuickView> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQuickView view; view.setSource(QUrl::fromLocalFile("application.qml")); view.show(); return app.exec(); }
This creates a QWindow-based view that displays the contents of application.qml
.
The application's .pro
project file must specify the declarative
module for the QT
variable. For example:
TEMPLATE += app QT += quick SOURCES += main.cpp
Creating a QQmlEngine directly
If application.qml
does not have any graphical components, or if it is preferred to avoid QQuickView for other reasons, the QQmlEngine can be constructed directly instead. In this case, application.qml
is loaded as a QQmlComponent instance rather than placed into a view:
#include <QGuiApplication> #include <QQmlEngine> #include <QQmlContext> #include <QQmlComponent> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlEngine engine; QQmlContext *objectContext = new QQmlContext(engine.rootContext()); QQmlComponent component(&engine, "application.qml"); QObject *object = component.create(objectContext); // ... delete object and objectContext when necessary return app.exec(); }
QGuiApplication can be replaced by a QCoreApplication in the code above in case you are not using any graphical items from Qt Quick. This allows using QML as a language without any dependencies to the Qt GUI module.
See 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 Qt's Resource system.
Managing Resource Files with the Qt Resource System
The Qt resource system allows resource files to be stored as binary files in an application executable. This can be useful when building a mixed QML/C++ application as it enables QML files (as well as other resources such as images and sound files) to be referred to through the resource system URI scheme rather than relative or absolute paths to filesystem resources. Note, however, that if you use the resource system, the application executable must be re-compiled whenever a QML source file is changed in order to update the resources in the package.
To use the resource system in a mixed QML/C++ application:
- Create a
.qrc
resource collection file that lists resource files in XML format - From C++, load the main QML file as a resource using the
:/
prefix or as a URL with theqrc
scheme
Once this is done, all files specified by relative paths in QML will be loaded from the resource system instead. Use of the resource system is completely transparent to the QML layer; this means all QML code should refer to resource files using relative paths and should not use the qrc
scheme. This scheme should only be used from C++ code for referring to resource files.
Here is a application packaged using the Qt resource system. The directory structure looks like this:
project |- example.qrc |- main.qml |- images |- background.png |- main.cpp |- project.pro
The main.qml
and background.png
files will be packaged as resource files. This is done in the example.qrc
resource collection file:
<!DOCTYPE RCC> <RCC version="1.0"> <qresource prefix="/"> <file>main.qml</file> <file>images/background.png</file> </qresource> </RCC>
Since background.png
is a resource file, main.qml
can refer to it using the relative path specified in example.qrc
:
// main.qml import QtQuick 2.3 Image { source: "images/background.png" }
To allow QML to locate resource files correctly, the main.cpp
loads the main QML file, main.qml
, as a resource file using the qrc
scheme:
int main(int argc, char *argv[]) { QApplication app(argc, argv); QQuickView view; view.setSource(QUrl("qrc:/main.qml")); view.show(); return app.exec(); }
Finally, project.pro
uses the RESOURCES variable to indicate that example.qrc
should be used to build the application resources:
QT += qml SOURCES += main.cpp RESOURCES += example.qrc
See The Qt Resource System for more information.