aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/doc/src/appdevguide/deployment.qdoc
blob: a137f4e97bab2a32069c4cf9d1fb3088e2ef34db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.  Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
\page qtquick-deployment.html
\title Deploying QML Applications
\brief process of 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 elements and plugin modules,
and it also provides access to third-party QML elements 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 \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 QML-based applications

To deploy 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:

\list
\li Loading the QML file through a QQuickView instance, or
\li Creating a QQmlEngine instance and loading QML files with QQmlComponent
\endlist


\section2 Deploying with QQuickView

QQuickView is a QWidget-based class that is able to load QML files.
For example, if there is a QML file, \c application.qml, like this:

\qml
    import QtQuick 2.0

    Rectangle { width: 100; height: 100; color: "red" }
\endqml

It can be loaded in a Qt application's \c main.cpp file like this:

\code
    #include <QApplication>
    #include <QQuickView>

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);

        QQuickView view;
        view.setSource(QUrl::fromLocalFile("application.qml"));
        view.show();

        return app.exec();
    }
\endcode

This creates a QWidget-based view that displays the contents of
\c application.qml.

The application's \c .pro \l{qmake Project Files}{project file} must specify
the \c declarative module for the \c QT variable. For example:

\code
    TEMPLATE += app
    QT += gui declarative
    SOURCES += main.cpp
\endcode


\section2 Creating a QQmlEngine directly

If \c 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, \c application.qml is
loaded as a QQmlComponent instance rather than placed into a view:

\code
    #include <QApplication>
    #include <QQmlEngine>
    #include <QQmlContext>
    #include <QQmlComponent>

    int main(int argc, char *argv[])
    {
        QApplication 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();
    }
\endcode

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 Developing and Prototyping with QML Scene

The Declarative UI package includes a QML runtime tool,
\l{qtquick-qmlscene.html}{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.



\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
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:

\list
\li Create a \c .qrc \l {The Qt Resource System}{resource collection file} that lists resource
   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
   \c qrc scheme
\endlist

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 \e not use the \c qrc scheme. This scheme should only be used from
C++ code for referring to resource files.

Here is a application packaged using the \l {The Qt Resource System}{Qt resource system}.
The directory structure looks like this:

\code
project
    |- example.qrc
    |- main.qml
    |- images
        |- background.png
    |- main.cpp
    |- project.pro
\endcode

The \c main.qml and \c background.png files will be packaged as resource files. This is
done in the \c example.qrc resource collection file:

\quotefile qml/qtbinding/resources/example.qrc

Since \c background.png is a resource file, \c main.qml can refer to it using the relative
path specified in \c example.qrc:

\snippet qml/qtbinding/resources/main.qml 0

To allow QML to locate resource files correctly, the \c main.cpp loads the main QML
file, \c main.qml, as a resource file using the \c qrc scheme:

\snippet qml/qtbinding/resources/main.cpp 0

Finally \c project.pro uses the RESOURCES variable to indicate that \c example.qrc should
be used to build the application resources:

\quotefile qml/qtbinding/resources/resources.pro

See \l {The Qt Resource System} for more information.