9d30ad3fcd0c8b1fe14b379663ec868321f0da40
[profile/ivi/qtdeclarative.git] / doc / src / declarative / qmlruntime.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the documentation of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:FDL$
10 ** GNU Free Documentation License
11 ** Alternatively, this file may be used under the terms of the GNU Free
12 ** Documentation License version 1.3 as published by the Free Software
13 ** Foundation and appearing in the file included in the packaging of
14 ** this file.
15 **
16 ** Other Usage
17 ** Alternatively, this file may be used in accordance with the terms
18 ** and conditions contained in a signed written agreement between you
19 ** and Nokia.
20 **
21 **
22 **
23 **
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29 \page qmlruntime.html
30 \inqmlmodule QtQuick 2
31 \title Qt Declarative UI Runtime
32
33 QML documents are loaded and executed by the QML runtime. This includes the
34 Declarative UI engine along with the built-in QML elements and plugin modules,
35 and it also provides access to third-party QML elements and modules.
36
37 Applications that use QML need to invoke the QML runtime in order to
38 execute QML documents. This can be done by creating a QDeclarativeView
39 or a QDeclarativeEngine, as described below. In addition, the Declarative UI
40 package includes the \QQV tool, which loads \c .qml files. This tool is
41 useful for developing and testing QML code without the need to write
42 a C++ application to load the QML runtime.
43
44
45
46 \section1 Deploying QML-based applications
47
48 To deploy an application that uses QML, the QML runtime must be invoked by
49 the application. This is done by writing a Qt C++ application that loads the
50 QDeclarativeEngine by either:
51
52 \list
53 \o Loading the QML file through a QDeclarativeView instance, or
54 \o Creating a QDeclarativeEngine instance and loading QML files with QDeclarativeComponent
55 \endlist
56
57
58 \section2 Deploying with QDeclarativeView
59
60 QDeclarativeView is a QWidget-based class that is able to load QML files.
61 For example, if there is a QML file, \c application.qml, like this:
62
63 \qml
64     import QtQuick 1.0
65
66     Rectangle { width: 100; height: 100; color: "red" }
67 \endqml
68
69 It can be loaded in a Qt application's \c main.cpp file like this:
70
71 \code
72     #include <QApplication>
73     #include <QDeclarativeView>
74
75     int main(int argc, char *argv[])
76     {
77         QApplication app(argc, argv);
78
79         QDeclarativeView view;
80         view.setSource(QUrl::fromLocalFile("application.qml"));
81         view.show();
82
83         return app.exec();
84     }
85 \endcode
86
87 This creates a QWidget-based view that displays the contents of
88 \c application.qml.
89
90 The application's \c .pro \l{qmake Project Files}{project file} must specify
91 the \c declarative module for the \c QT variable. For example:
92
93 \code
94     TEMPLATE += app
95     QT += gui declarative
96     SOURCES += main.cpp
97 \endcode
98
99
100 \section2 Creating a QDeclarativeEngine directly
101
102 If \c application.qml does not have any graphical components, or if it is
103 preferred to avoid QDeclarativeView for other reasons, the QDeclarativeEngine
104 can be constructed directly instead. In this case, \c application.qml is
105 loaded as a QDeclarativeComponent instance rather than placed into a view:
106
107 \code
108     #include <QApplication>
109     #include <QDeclarativeEngine>
110     #include <QDeclarativeContext>
111     #include <QDeclarativeComponent>
112
113     int main(int argc, char *argv[])
114     {
115         QApplication app(argc, argv);
116
117         QDeclarativeEngine engine;
118         QDeclarativeContext *objectContext = new QDeclarativeContext(engine.rootContext());
119
120         QDeclarativeComponent component(&engine, "application.qml");
121         QObject *object = component.create(objectContext);
122
123         // ... delete object and objectContext when necessary
124
125         return app.exec();
126     }
127 \endcode
128
129 See \l {Using QML Bindings in C++ Applications} for more information about using
130 QDeclarativeEngine, QDeclarativeContext and QDeclarativeComponent, as well
131 as details on including QML files through \l{The Qt Resource System}{Qt's Resource system}.
132
133
134
135 \section1 Developing and prototyping with QML Viewer
136
137 The Declarative UI package includes a QML runtime tool, the \QQV, which loads
138 and displays QML documents. This is useful during the application development
139 phase for prototyping QML-based applications without writing your own C++
140 applications to invoke the QML runtime.
141
142 See the \l{QML Viewer} documentation for more details.
143
144 */
145