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
definetypes.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3/*!
4\page qtqml-documents-definetypes.html
5\meta {keywords} {qmltopic}
6\title Defining Object Types through QML Documents
7\brief Description of how a QML document is a reusable type definition
8
9One of the core features of QML is that it enables QML object types to be
10easily defined in a lightweight manner through QML documents to suit the needs
11of individual QML applications. The standard \l {Qt Quick} module provides
12various types like \l Rectangle, \l Text and \l Image for building a QML
13application; beyond these, you can easily define your own QML types to be
14reused within your application. This ability to create your own types forms the
15building blocks of any QML application.
16
17
18\section1 Defining an Object Type with a QML File
19
20\section2 Naming Custom QML Object Types
21
22To create an object type, a QML document should be placed into a text file
23named as \e <TypeName>.qml where \e <TypeName> is the desired name of the type.
24The type name has the following requirements:
25
26\list
27 \li It must be comprised of alphanumeric characters or underscores.
28 \li It must begin with an uppercase letter.
29\endlist
30
31This document is then automatically recognized by the engine as a definition of
32a QML type. Additionally, a type defined in this manner is automatically made
33available to other QML files within the same local directory as the engine
34searches within the immediate directory when resolving QML type names.
35
36\note The QML engine does not automatically search remote directories this way.
37You have to add a qmldir file if your documents are loaded over the network. See
38\l{Importing QML Document Directories}.
39
40\section2 Custom QML Type Definition
41
42For example, below is a document that declares a \l Rectangle with a child \l
43MouseArea. The document has been saved to file named \c SquareButton.qml:
44
45\qml
46// SquareButton.qml
47import QtQuick 2.0
48
49Rectangle {
50 property int side: 100
51 width: side; height: side
52 color: "red"
53
54 MouseArea {
55 anchors.fill: parent
56 onClicked: console.log("Button clicked!")
57 }
58}
59\endqml
60
61Since the file is named \c SquareButton.qml, \b {this can now be used as a type
62named \c SquareButton by any other QML file within the same directory}. For
63example, if there was a \c myapplication.qml file in the same directory, it
64could refer to the \c SquareButton type:
65
66\qml
67// myapplication.qml
68import QtQuick 2.0
69
70SquareButton {}
71\endqml
72
73\image documents-definetypes-simple.png {The square button type in
74 myapplication.qml inherits properties defined in SquareButton.qml.}
75
76This creates a 100 x 100 red \l Rectangle with an inner \l MouseArea, as
77defined in \c SquareButton.qml. When this \c myapplication.qml document is
78loaded by the engine, it loads the SquareButton.qml document as a component and
79instantiates it to create a \c SquareButton object.
80
81The \c SquareButton type encapsulates the tree of QML objects declared in \c
82SquareButton.qml. When the QML engine instantiates a \c SquareButton object
83from this type, it is instantiating an object from the \l Rectangle tree
84declared in \c SquareButton.qml.
85
86\note the letter case of the file name is significant on some (notably UNIX)
87filesystems. It is recommended the file name case matches the case of the
88desired QML type name exactly - for example, \c Box.qml and not \c BoX.qml -
89regardless of the platform to which the QML type will be deployed.
90
91\section2 Inline Components
92
93Sometimes, it can be inconvenient to create a new file for a type, for
94instance when reusing a small delegate in multiple views. If you don't actually
95need to expose the type, but only need to create an instance,
96\l{QtQml::Component}{Component} is an option.
97But if you want to declare properties with the component types, or if you
98want to use it in multiple files, \c Component is not an option. In that case,
99you can use \e {inline components}. Inline components declare a new component
100inside of a file. The syntax for that is
101\code
102component <component name> : BaseType {
103 // declare properties and bindings here
104}
105\endcode
106
107Inside the file which declares the inline component, the type can be referenced
108simply by its name.
109
110\snippet qml/qml-documents/Images.qml document
111
112In other files, it has to be prefixed with the name of its containing component.
113
114\snippet qml/qml-documents/LabeledImageBox.qml document
115
116\note Inline components don't share their scope with the component they are
117declared in. In the following example, when \c A.MyInlineComponent in file
118B.qml gets created, a ReferenceError will occur, as \c root does not exist as
119an id in B.qml.
120It is therefore advisable not to reference objects in an inline component
121which are not part of it.
122
123\snippet qml/qml-documents/A.qml document
124\snippet qml/qml-documents/B.qml document
125
126\note Inline components cannot be nested.
127
128\section2 Importing Types Defined Outside the Current Directory
129
130If \c SquareButton.qml was not in the same directory as \c myapplication.qml,
131the \c SquareButton type would need to be specifically made available through
132an \e import statement in \c myapplication.qml. It could be imported from a
133relative path on the file system, or as an installed module; see \l {QML
134Modules}{module} for more details.
135
136
137\section1 Accessible Attributes of Custom Types
138
139The \b {root object} definition in a .qml file \b {defines the attributes that
140are available for a QML type}. All properties, signals and methods that belong
141to this root object - whether they are custom declared, or come from the QML
142type of the root object - are externally accessible and can be read and
143modified for objects of this type.
144
145For example, the root object type in the \c SquareButton.qml file above is \l
146Rectangle. This means any properties defined by the \l Rectangle type can be
147modified for a \c SquareButton object. The code below defines three \c
148SquareButton objects with customized values for some of the properties of the
149root \l Rectangle object of the \c SquareButton type:
150
151\qml
152// application.qml
153import QtQuick 2.0
154
155Column {
156 SquareButton { side: 50 }
157 SquareButton { x: 50; color: "blue" }
158 SquareButton { radius: 10 }
159}
160\endqml
161
162\image documents-definetypes-attributes.png {Column containing three square
163 buttons with different color and size properties}
164
165The attributes that are accessible to objects of the custom QML type include
166any \l{Defining Property Attributes}{custom properties}, \l{Defining Method
167Attributes}{methods} and \l{Defining Signal Attributes}{signals} that have
168additionally been defined for an object. For example, suppose the \l Rectangle
169in \c SquareButton.qml had been defined as follows, with additional properties,
170methods and signals:
171
172\qml
173// SquareButton.qml
174import QtQuick 2.0
175
176Rectangle {
177 id: root
178
179 property bool pressed: mouseArea.pressed
180
181 signal buttonClicked(real xPos, real yPos)
182
183 function randomizeColor() {
184 root.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
185 }
186
187 property int side: 100
188 width: side; height: side
189 color: "red"
190
191 MouseArea {
192 id: mouseArea
193 anchors.fill: parent
194 onClicked: (mouse)=> root.buttonClicked(mouse.x, mouse.y)
195 }
196}
197\endqml
198
199Any \c SquareButton object could make use of the \c pressed property, \c
200buttonClicked signal and \c randomizeColor() method that have been added to the
201root \l Rectangle:
202
203\qml
204// application.qml
205import QtQuick 2.0
206
207SquareButton {
208 id: squareButton
209
210 onButtonClicked: (xPos, yPos)=> {
211 console.log("Clicked", xPos, yPos)
212 randomizeColor()
213 }
214
215 Text { text: squareButton.pressed ? "Down" : "Up" }
216}
217\endqml
218
219Note that any of the \c id values defined in \c SquareButton.qml are not
220accessible to \c SquareButton objects, as id values are only accessible from
221within the component scope in which a component is declared. The \c
222SquareButton object definition above cannot refer to \c mouseArea in order to
223refer to the \l MouseArea child, and if it had an \c id of \c root rather than
224\c squareButton, this would not conflict with the \c id of the same value for
225the root object defined in \c SquareButton.qml as the two would be declared
226within separate scopes.
227*/