source: trunk/doc/src/declarative/qmlruntime.qdoc@ 980

Last change on this file since 980 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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