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
qml.qdoc
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page qtquick-qml-runtime.html
6 \title Prototyping with the QML Runtime Tool
7 \target qml_runtime_tool
8 \ingroup qtqml-tools
9 \ingroup qttools
10
11 \brief Utility to test and load QML files
12 Qt includes the \c qml executable, a utility that loads and displays QML
13 documents.
14
15 The \c qml utility is meant mainly for testing your QML applications and
16 components. To launch a QML application in a production environment, often
17 you need to develop a custom C++ application, or bundle the QML file in a
18 module. See \l {Deploying QML applications} for more information.
19
20 \c qml can instantiate any QML type you provide, as is, even if there is
21 nothing to display. To display QML content with the root object other than
22 \l Window, \c qml wraps this content in a \l Window.
23
24 Therefore, when moving from a prototype developed with \c qml to
25 a C++ application, you need to either make sure the root element is a
26 \l Window, or create a \l QQuickView in C++ to hold the root \l Item.
27 But in the meantime, you can load and test parts of your prototype
28 separately with the \c qml tool.
29
30 To load a .qml file, provide the file path on the command prompt:
31
32 \code
33 $ qml myqmlfile.qml
34 \endcode
35
36 To see the configuration options, run \c qml with the \c --help argument.
37
38 When the root object in the QML file that you are loading is an Item
39 rather than a Window, it needs to be wrapped in a \l Window to be shown.
40 While this work is pending, the top-level object that is already loaded
41 is represented by a \c PartialScene object. The \c qml tool then loads
42 additional QML files to decide what to do next: one is a configuration
43 file that specifies in what sort of container to wrap the \c PartialScene.
44 The \c PartialScene.container property gives a URL pointing to QML source
45 code for the container component, which normally should declare a \l Window
46 in which to wrap the \l Item that was loaded first. Thus, the process of
47 wrapping an Item into a Window is programmable; and by default, these
48 two additional QML files are loaded from resources inside the qml
49 executable. You can list the available configurations with the
50 \c --list-conf command:
51
52 \code
53 $ qml --list-conf
54 Built-in configurations:
55 default
56 resizeToItem
57 \endcode
58
59 The \c default configuration provides default behavior: the root Item will
60 be resized to fill the wrapper Window at startup, and also when the user
61 resizes the window. The alternative \c resizeToItem configuration works the
62 other way around: the Item can programmatically set its own size (for
63 example by creating bindings to its own \c width and \c height properties),
64 and the wrapper Window will be resized to fit (subject to any limits
65 that may be imposed by the window system). You can choose either of
66 these using the \c -c or \c --config option:
67
68 \code
69 $ qml -c resizeToItem selfResizingItem.qml
70 \endcode
71
72 Additional configurations can be added by creating configuration
73 directories in \l QStandardPaths::AppConfigLocation, each with two QML
74 files inside: a configuration file named \c configuration.qml, and a QML
75 file that declares the Item wrapper, which can have any name. If this has
76 been done, the \c {qml --list-conf} command will also list those extra
77 configurations, while the \c --verbose option will expand those to give the
78 complete paths to those configurations, and the additional locations that
79 were searched:
80
81 \code
82 $ qml --list-conf --verbose
83 Built-in configurations:
84 default
85 resizeToItem
86 Other configurations:
87 /home/myuser/.config/QtProject/Qml Runtime/simplest
88 Checked in:
89 /home/myuser/.config/QtProject/Qml Runtime
90 /etc/xdg/QtProject/Qml Runtime
91 \endcode
92
93 Here is an example \c configuration.qml file:
94
95 \qml
96 import QmlRuntime.Config
97
98 Configuration {
99 PartialScene {
100 itemType: "QQuickItem"
101 container: Qt.resolvedUrl("ItemWrapper.qml")
102 }
103 }
104 \endqml
105
106 And here is the simplest-possible \c ItemWrapper.qml that the \c container
107 property could point to:
108
109 \qml
110 import QtQuick
111
112 Window {
113 required property Item containedObject: null
114 onContainedObjectChanged: {
115 if (containedObject == undefined || containedObject == null) {
116 visible = false;
117 } else {
118 containedObject.parent = contentItem;
119 visible = true;
120 }
121 }
122 }
123 \endqml
124
125 When these files have been created, you can use the \c {qml -c} option
126 giving the name of the directory containing the \c configuration.qml file,
127 which specifies the path to the container object:
128
129 \code
130 $ qml -c simplest mycomponent.qml
131 \endcode
132
133 The \c qml runtime will directly set the \c containedObject property, which
134 is required to have that name; and when it is set, the \l Item will be
135 reparented to the \l Window and shown. Since this Window is declared in
136 QML, when you write your own wrapper window, you are free to add whatever
137 additional features you would like: to handle resizing in a customized
138 way, or to add capabilities that you may find useful during prototyping.
139
140 Regardless of what was found in \c AppConfigLocation, you can alternatively
141 use the \c {qml -c} option giving the complete path to the
142 \c configuration.qml file, and it can in turn specify the complete path to
143 the container object; so these files can be located anywhere.
144
145 In addition to the features that can be declared in the configuration files,
146 the \c qml tool also provides a few more features via command-line options.
147 Use the \c --help option to get an up-to-date list.
148*/