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
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
10
A QML object type is a type from which a QML object can be instantiated.
11
12
In syntactic terms, a QML object type is one which can be used to declare an
13
object by specifying the \e{type name} followed by a set of curly braces that
14
encompasses the attributes of that object. This differs from \e {value types},
15
which cannot be used in the same way. For example, \l Rectangle is a QML object
16
type: it can be used to create \c Rectangle type objects. This cannot be done
17
with primitive types such as \c int and \c bool, which are used to hold simple
18
data types rather than objects.
19
20
Custom QML object types can be defined by creating a .qml file that defines the
21
type, as discussed in \l {qtqml-documents-definetypes.html}
22
{Documents as QML object type definitions}, or by defining a QML type from C++
23
and registering the type with the QML engine, as discussed in
24
\l{qtqml-cppintegration-definetypes.html}{Defining QML Types from C++}.
25
Note that in both cases, the type name must begin with an uppercase letter in
26
order to be declared as a QML object type in a QML file.
27
28
For more information about C++ and the different QML integration methods,
29
see the
30
\l {Overview - QML and C++ Integration} {C++ and QML integration overview} page.
31
32
There are two built-in object types that can be used without importing any other
33
modules: \l{QtObject} is the base type of all object types. \l{Component} can be
34
used to define new object types inline in QML documents.
35
36
\section1 Defining Object Types from QML
37
38
39
\section2 Defining Object Types Through QML Documents
40
41
Plugin writers and application developers may provide types defined as QML
42
documents. A QML document, when visible to the QML import system, defines a
43
type identified by the name of the file minus the file extensions.
44
45
Thus, if a QML document named "MyButton.qml" exists, it provides the definition
46
of the "MyButton" type, which may be used in a QML application.
47
48
See the documentation about \l{QML Documents} for
49
information on how to define a QML document, and the syntax of the QML
50
language. Once you are familiar with the QML language and how to define QML
51
documents, see the documentation which explains how to
52
\l{qtqml-documents-definetypes.html}
53
{define and use your own reusable QML types in QML documents}.
54
55
See \l {Defining Object Types through QML Documents} for more information.
56
57
58
59
\section2 Defining Anonymous Types with Component
60
61
Another method of creating object types from within QML is to use the \l Component type.
62
This allows a type to be defined inline within a QML document, instead of using a separate
63
document in a \c .qml file.
64
65
\qml
66
Item {
67
id: root
68
width: 500; height: 500
69
70
Component {
71
id: myComponent
72
Rectangle { width: 100; height: 100; color: "red" }
73
}
74
75
Component.onCompleted: {
76
myComponent.createObject(root)
77
myComponent.createObject(root, {"x": 200})
78
}
79
}
80
\endqml
81
82
Here the \c myComponent object essentially defines an anonymous type that can be instantiated
83
using \l {Component::createObject} to create objects of this anonymous type.
84
85
86
Inline components share all
87
the characteristics of regular top-level components and use the same \c import
88
list as their containing QML document.
89
90
91
92
Note that each \l Component object declaration creates its own \e {component scope}. Any
93
\e id values used and referred to from within a \l Component object declaration must be
94
unique within that scope, but do not need to be unique within the document within which the
95
inline component is declared. So, the \l Rectangle declared in the \c myComponent object
96
declaration could have an \e id of \c root without conflicting with the \c root declared
97
for the \l Item object in the same document, as these two \e id values are declared within
98
different component scopes.
99
100
See \l{qtqml-documents-scope.html}{Scope and Naming Resolution} for more details.
101
102
103
\section1 Defining Object Types from C++
104
105
C++ plugin writers and application developers may register types defined in C++
106
through API provided by the Qt Qml module. There are various registration
107
functions which each allow different use-cases to be fulfilled.
108
For more information about those registration functions, and the specifics of
109
exposing custom C++ types to QML, see the documentation regarding
110
\l{qtqml-cppintegration-definetypes.html}{Defining QML Types from C++}.
111
112
The QML type-system relies on imports, plugins and extensions being installed
113
into a known import path. Plugins may be provided by third-party developers
114
and reused by client application developers. Please see the documentation
115
about \l{qtqml-modules-topic.html}{QML modules} for more information about
116
how to create and deploy a QML extension module.
117
118
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
typesystem
objecttypes.qdoc
Generated on
for Qt by
1.16.1