1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
30 \title Qt Declarative UI Runtime
32 QML documents are loaded and executed by the QML runtime. This includes the
33 Declarative UI engine along with the built-in QML elements and plugin modules,
34 and it also provides access to third-party QML elements and modules.
36 Applications that use QML need to invoke the QML runtime in order to
37 execute QML documents. This can be done by creating a QDeclarativeView
38 or a QDeclarativeEngine, as described below. In addition, the Declarative UI
39 package includes the \QQV tool, which loads \c .qml files. This tool is
40 useful for developing and testing QML code without the need to write
41 a C++ application to load the QML runtime.
45 \section1 Deploying QML-based applications
47 To deploy an application that uses QML, the QML runtime must be invoked by
48 the application. This is done by writing a Qt C++ application that loads the
49 QDeclarativeEngine by either:
52 \o Loading the QML file through a QDeclarativeView instance, or
53 \o Creating a QDeclarativeEngine instance and loading QML files with QDeclarativeComponent
57 \section2 Deploying with QDeclarativeView
59 QDeclarativeView is a QWidget-based class that is able to load QML files.
60 For example, if there is a QML file, \c application.qml, like this:
65 Rectangle { width: 100; height: 100; color: "red" }
68 It can be loaded in a Qt application's \c main.cpp file like this:
71 #include <QApplication>
72 #include <QDeclarativeView>
74 int main(int argc, char *argv[])
76 QApplication app(argc, argv);
78 QDeclarativeView view;
79 view.setSource(QUrl::fromLocalFile("application.qml"));
86 This creates a QWidget-based view that displays the contents of
89 The application's \c .pro \l{qmake Project Files}{project file} must specify
90 the \c declarative module for the \c QT variable. For example:
99 \section2 Creating a QDeclarativeEngine directly
101 If \c application.qml does not have any graphical components, or if it is
102 preferred to avoid QDeclarativeView for other reasons, the QDeclarativeEngine
103 can be constructed directly instead. In this case, \c application.qml is
104 loaded as a QDeclarativeComponent instance rather than placed into a view:
107 #include <QApplication>
108 #include <QDeclarativeEngine>
109 #include <QDeclarativeContext>
110 #include <QDeclarativeComponent>
112 int main(int argc, char *argv[])
114 QApplication app(argc, argv);
116 QDeclarativeEngine engine;
117 QDeclarativeContext *objectContext = new QDeclarativeContext(engine.rootContext());
119 QDeclarativeComponent component(&engine, "application.qml");
120 QObject *object = component.create(objectContext);
122 // ... delete object and objectContext when necessary
128 See \l {Using QML Bindings in C++ Applications} for more information about using
129 QDeclarativeEngine, QDeclarativeContext and QDeclarativeComponent, as well
130 as details on including QML files through \l{The Qt Resource System}{Qt's Resource system}.
134 \section1 Developing and prototyping with QML Viewer
136 The Declarative UI package includes a QML runtime tool, the \QQV, which loads
137 and displays QML documents. This is useful during the application development
138 phase for prototyping QML-based applications without writing your own C++
139 applications to invoke the QML runtime.
141 See the \l{QML Viewer} documentation for more details.