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
9
One of the core features of QML is that it enables QML object types to be
10
easily defined in a lightweight manner through QML documents to suit the needs
11
of individual QML applications. The standard \l {Qt Quick} module provides
12
various types like \l Rectangle, \l Text and \l Image for building a QML
13
application; beyond these, you can easily define your own QML types to be
14
reused within your application. This ability to create your own types forms the
15
building 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
22
To create an object type, a QML document should be placed into a text file
23
named as \e <TypeName>.qml where \e <TypeName> is the desired name of the type.
24
The 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
31
This document is then automatically recognized by the engine as a definition of
32
a QML type. Additionally, a type defined in this manner is automatically made
33
available to other QML files within the same local directory as the engine
34
searches within the immediate directory when resolving QML type names.
35
36
\note The QML engine does not automatically search remote directories this way.
37
You 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
42
For example, below is a document that declares a \l Rectangle with a child \l
43
MouseArea. The document has been saved to file named \c SquareButton.qml:
44
45
\qml
46
// SquareButton.qml
47
import QtQuick 2.0
48
49
Rectangle {
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
61
Since the file is named \c SquareButton.qml, \b {this can now be used as a type
62
named \c SquareButton by any other QML file within the same directory}. For
63
example, if there was a \c myapplication.qml file in the same directory, it
64
could refer to the \c SquareButton type:
65
66
\qml
67
// myapplication.qml
68
import QtQuick 2.0
69
70
SquareButton {}
71
\endqml
72
73
\image documents-definetypes-simple.png {The square button type in
74
myapplication.qml inherits properties defined in SquareButton.qml.}
75
76
This creates a 100 x 100 red \l Rectangle with an inner \l MouseArea, as
77
defined in \c SquareButton.qml. When this \c myapplication.qml document is
78
loaded by the engine, it loads the SquareButton.qml document as a component and
79
instantiates it to create a \c SquareButton object.
80
81
The \c SquareButton type encapsulates the tree of QML objects declared in \c
82
SquareButton.qml. When the QML engine instantiates a \c SquareButton object
83
from this type, it is instantiating an object from the \l Rectangle tree
84
declared in \c SquareButton.qml.
85
86
\note the letter case of the file name is significant on some (notably UNIX)
87
filesystems. It is recommended the file name case matches the case of the
88
desired QML type name exactly - for example, \c Box.qml and not \c BoX.qml -
89
regardless of the platform to which the QML type will be deployed.
90
91
\section2 Inline Components
92
93
Sometimes, it can be inconvenient to create a new file for a type, for
94
instance when reusing a small delegate in multiple views. If you don't actually
95
need to expose the type, but only need to create an instance,
96
\l{QtQml::Component}{Component} is an option.
97
But if you want to declare properties with the component types, or if you
98
want to use it in multiple files, \c Component is not an option. In that case,
99
you can use \e {inline components}. Inline components declare a new component
100
inside of a file. The syntax for that is
101
\code
102
component <component name> : BaseType {
103
// declare properties and bindings here
104
}
105
\endcode
106
107
Inside the file which declares the inline component, the type can be referenced
108
simply by its name.
109
110
\snippet qml/qml-documents/Images.qml document
111
112
In 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
117
declared in. In the following example, when \c A.MyInlineComponent in file
118
B.qml gets created, a ReferenceError will occur, as \c root does not exist as
119
an id in B.qml.
120
It is therefore advisable not to reference objects in an inline component
121
which 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
130
If \c SquareButton.qml was not in the same directory as \c myapplication.qml,
131
the \c SquareButton type would need to be specifically made available through
132
an \e import statement in \c myapplication.qml. It could be imported from a
133
relative path on the file system, or as an installed module; see \l {QML
134
Modules}{module} for more details.
135
136
137
\section1 Accessible Attributes of Custom Types
138
139
The \b {root object} definition in a .qml file \b {defines the attributes that
140
are available for a QML type}. All properties, signals and methods that belong
141
to this root object - whether they are custom declared, or come from the QML
142
type of the root object - are externally accessible and can be read and
143
modified for objects of this type.
144
145
For example, the root object type in the \c SquareButton.qml file above is \l
146
Rectangle. This means any properties defined by the \l Rectangle type can be
147
modified for a \c SquareButton object. The code below defines three \c
148
SquareButton objects with customized values for some of the properties of the
149
root \l Rectangle object of the \c SquareButton type:
150
151
\qml
152
// application.qml
153
import QtQuick 2.0
154
155
Column {
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
165
The attributes that are accessible to objects of the custom QML type include
166
any \l{Defining Property Attributes}{custom properties}, \l{Defining Method
167
Attributes}{methods} and \l{Defining Signal Attributes}{signals} that have
168
additionally been defined for an object. For example, suppose the \l Rectangle
169
in \c SquareButton.qml had been defined as follows, with additional properties,
170
methods and signals:
171
172
\qml
173
// SquareButton.qml
174
import QtQuick 2.0
175
176
Rectangle {
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
199
Any \c SquareButton object could make use of the \c pressed property, \c
200
buttonClicked signal and \c randomizeColor() method that have been added to the
201
root \l Rectangle:
202
203
\qml
204
// application.qml
205
import QtQuick 2.0
206
207
SquareButton {
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
219
Note that any of the \c id values defined in \c SquareButton.qml are not
220
accessible to \c SquareButton objects, as id values are only accessible from
221
within the component scope in which a component is declared. The \c
222
SquareButton object definition above cannot refer to \c mouseArea in order to
223
refer 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
225
the root object defined in \c SquareButton.qml as the two would be declared
226
within separate scopes.
227
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
documents
definetypes.qdoc
Generated on
for Qt by
1.16.1