Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
objecttypes.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-typesystem-objecttypes.html
5
\meta {keywords} {qmltopic}
6
\title QML Object Types
7
\brief describes QML object types and how to create them.
8
9
A QML object type is a type from which a QML object can be instantiated.
10
11
Unlike \l{QML Value Types}{value types}, object types are passed by reference.
12
If you assign an instance of an object type to two different properties, both
13
properties carry the same value. If the object is modified, the change is
14
reflected in both properties.
15
16
In syntactic terms, a QML object type is one which can be used to create an
17
instance of an object by specifying the \e{type name} followed by a set of
18
curly braces that contains the attributes of that object.
19
For example, \l Rectangle is a QML object type: it can be used to create
20
\c Rectangle type objects, like this one:
21
22
\qml
23
Rectangle {
24
width: 200
25
height: 200
26
color: "blue"
27
}
28
\endqml
29
30
\e {Value types}, such as the \c int and \c bool primitive types cannot be used
31
in the same way. These are used to hold simple values rather than objects.
32
33
There are two built-in object types that can be used without importing any other
34
modules: \l{QtObject} is the base type of all object types. \l{Component} can be
35
used to define new object types inline in QML documents.
36
37
\section1 Defining Object Types
38
39
Custom QML object types can be defined in several different ways, enabling each
40
new type to be developed with an appropriate amount of effort for each use
41
case, using the most suitable tools and programming language to implement it.
42
43
\section2 Defining Object Types with QML Documents
44
45
Object types can be defined by creating a QML document in a .qml file that
46
defines the type. A QML document, when visible to the QML import system, usually
47
defines a type identified by the name of the file minus the file extensions. A
48
qmldir file can override this.
49
50
For example, a QML document named "MyButton.qml" provides the definition of the
51
"MyButton" type, which may be used in a QML application.
52
53
See the documentation about \l{QML Documents} for information on how to define
54
a QML document, and the syntax of the QML language. Once you are familiar with
55
the QML language and how to define QML documents, see
56
\l {Defining Object Types with QML Documents} for more information.
57
58
\section2 Defining Anonymous Types with Component
59
60
Another method of creating object types from within QML is to use the
61
\l Component type. This allows a type to be defined inline within a QML
62
document, instead of using a separate document in a \c .qml file.
63
64
\qml
65
Item {
66
id: root
67
width: 500; height: 500
68
69
Component {
70
id: myComponent
71
Rectangle { width: 100; height: 100; color: "red" }
72
}
73
74
Component.onCompleted: {
75
myComponent.createObject(root)
76
myComponent.createObject(root, {"x": 200})
77
}
78
}
79
\endqml
80
81
Here, the \c myComponent object essentially defines an anonymous type that can
82
be instantiated using \l {Component::createObject} to create objects of this
83
anonymous type.
84
85
Inline components share all the characteristics of regular top-level components
86
and use the same \c import list as their containing QML document.
87
88
Note that each \l Component object declaration creates its own
89
\e {component scope}. Any \e id values used and referred to from within a
90
\l Component object declaration must be unique within that scope, but do not
91
need to be unique within the document in which the inline component is declared.
92
So, the \l Rectangle declared in the \c myComponent object declaration could
93
have an \e id of \c root without conflicting with the \c root declared for the
94
\l Item object in the same document, as these two \e id values are declared within
95
different component scopes.
96
97
See \l{Scope and Naming Resolution} for more details.
98
99
\section2 Defining Object Types from C++
100
101
C++ plugin writers and application developers may register types defined in C++
102
through APIs provided by the Qt Qml module. There are various registration
103
functions which each allow different use-cases to be fulfilled.
104
For more information about those registration functions, and the specifics of
105
exposing custom C++ types to QML, see the documentation regarding
106
\l{Defining QML Types from C++}.
107
108
The QML type-system relies on QML modules being installed
109
into a known import path. QML modules may be provided by third-party developers
110
and reused by client application developers. Please see the documentation
111
about \l{QML Modules}{QML modules} for more information about how to create and
112
deploy a QML extension module.
113
114
For more information about the different C++ and QML integration methods, see
115
the \l{Overview - QML and C++ Integration}{C++ and QML integration overview}
116
page.
117
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
typesystem
objecttypes.qdoc
Generated on
for Qt by
1.18.0