Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
initializing-qml-runtime.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtqml-initializing-qml-runtime.html
6
\title Initializing the QML Runtime
7
\brief Provides information on how to initialize the QML runtime in applications.
8
9
QML documents are loaded and run by the QML runtime. The
10
QML runtime also provides access to other Qt or third-party QML modules
11
and types.
12
13
In addition, the Qt Framework provides the \c qml tool, which loads
14
\c {.qml} files. This tool is useful for developing and testing QML code
15
without having to write a C++ application to load the QML runtime.
16
17
To run an application that uses QML, your application must invoke the QML
18
runtime. To invoke the QML runtime:
19
20
\list
21
\li Create a Qt C++ application.
22
23
The application should load the QQmlEngine in one of the following ways:
24
\list
25
\li Loading the QML file through a QQuickView instance.
26
\li Creating a QQmlEngine instance and loading QML files with
27
QQmlComponent.
28
\endlist
29
\li Create a Qt Bridges application.
30
31
For details, see Qt Bridges documentation:
32
\list
33
\li \l{Qt Bridges for C#}
34
\li \l{Qt Bridges for Rust}
35
\endlist
36
\li Use the \c qml tool to load and run QML files directly.
37
38
This tool is useful for developing and testing QML code without having
39
to write an application to load the QML runtime. For details, see
40
\l{Prototyping with the QML Runtime Tool}.
41
\endlist
42
43
\section1 Initializing the QML Runtime in C++ Applications
44
45
\section2 Initializing with QQuickView
46
47
\l QQuickView is a QWindow-based class that can load QML files. For example, if
48
there is a QML file, \c application.qml, it will look like this:
49
50
\qml
51
import QtQuick
52
53
Rectangle { width: 100; height: 100; color: "red" }
54
\endqml
55
56
It can be loaded in a Qt application's \c main.cpp file like this:
57
58
\code
59
#include <QGuiApplication>
60
#include <QQuickView>
61
62
int main(int argc, char *argv[])
63
{
64
QGuiApplication app(argc, argv);
65
66
QQuickView view;
67
view.setSource(QUrl::fromLocalFile("application.qml"));
68
view.show();
69
70
return app.exec();
71
}
72
\endcode
73
74
This creates a QWindow-based view that displays the contents of
75
\c {application.qml}.
76
77
\section3 Build files
78
79
\if defined(onlinedocs)
80
\tab {build-qt-app}{tab-cmake}{CMake}{checked}
81
\tab {build-qt-app}{tab-qmake}{qmake}{}
82
\tabcontent {tab-cmake}
83
\else
84
\section4 Using CMake
85
\endif
86
\include {module-use.qdocinc} {building with cmake} {Quick}
87
\if defined(onlinedocs)
88
\endtabcontent
89
\tabcontent {tab-qmake}
90
\else
91
\section4 Using qmake
92
\endif
93
\include {module-use.qdocinc} {building_with_qmake} {quick}
94
For more information, see \l{Creating Project Files}.
95
\if defined(onlinedocs)
96
\endtabcontent
97
\endif
98
99
\section2 Creating a QQmlEngine directly
100
101
If \c application.qml doesn't have any graphical components, or if it's
102
preferred to avoid \l QQuickView for other reasons, the \l QQmlEngine can be
103
constructed directly instead. In this case, \c application.qml is loaded as a
104
\l QQmlComponent instance rather than placed into a view:
105
106
\code
107
#include <QGuiApplication>
108
#include <QQmlEngine>
109
#include <QQmlContext>
110
#include <QQmlComponent>
111
112
int main(int argc, char *argv[])
113
{
114
QGuiApplication app(argc, argv);
115
116
QQmlEngine engine;
117
QQmlContext *objectContext = new QQmlContext(engine.rootContext());
118
119
QQmlComponent 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
128
If you're not using any graphical items from Qt Quick, you can replace
129
\l QGuiApplication with a \l QCoreApplication in the code above. This way, you
130
can use QML as a language without any dependencies to the \l{Qt GUI} module.
131
132
*/
qtdeclarative
src
qml
doc
src
initializing-qml-runtime.qdoc
Generated on
for Qt by
1.16.1