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