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
9A QML object type is a type from which a QML object can be instantiated.
10
11Unlike \l{QML Value Types}{value types}, object types are passed by reference.
12If you assign an instance of an object type to two different properties, both
13properties carry the same value. If the object is modified, the change is
14reflected in both properties.
15
16In syntactic terms, a QML object type is one which can be used to create an
17instance of an object by specifying the \e{type name} followed by a set of
18curly braces that contains the attributes of that object.
19For 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
23Rectangle {
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
31in the same way. These are used to hold simple values rather than objects.
32
33There are two built-in object types that can be used without importing any other
34modules: \l{QtObject} is the base type of all object types. \l{Component} can be
35used to define new object types inline in QML documents.
36
37\section1 Defining Object Types
38
39Custom QML object types can be defined in several different ways, enabling each
40new type to be developed with an appropriate amount of effort for each use
41case, using the most suitable tools and programming language to implement it.
42
43\section2 Defining Object Types with QML Documents
44
45Object types can be defined by creating a QML document in a .qml file that
46defines the type. A QML document, when visible to the QML import system, usually
47defines a type identified by the name of the file minus the file extensions. A
48qmldir file can override this.
49
50For example, a QML document named "MyButton.qml" provides the definition of the
51"MyButton" type, which may be used in a QML application.
52
53See the documentation about \l{QML Documents} for information on how to define
54a QML document, and the syntax of the QML language. Once you are familiar with
55the 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
60Another 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
62document, instead of using a separate document in a \c .qml file.
63
64\qml
65Item {
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
81Here, the \c myComponent object essentially defines an anonymous type that can
82be instantiated using \l {Component::createObject} to create objects of this
83anonymous type.
84
85Inline components share all the characteristics of regular top-level components
86and use the same \c import list as their containing QML document.
87
88Note 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
91need to be unique within the document in which the inline component is declared.
92So, the \l Rectangle declared in the \c myComponent object declaration could
93have 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
95different component scopes.
96
97See \l{Scope and Naming Resolution} for more details.
98
99\section2 Defining Object Types from C++
100
101C++ plugin writers and application developers may register types defined in C++
102through APIs provided by the Qt Qml module. There are various registration
103functions which each allow different use-cases to be fulfilled.
104For more information about those registration functions, and the specifics of
105exposing custom C++ types to QML, see the documentation regarding
106\l{Defining QML Types from C++}.
107
108The QML type-system relies on QML modules being installed
109into a known import path. QML modules may be provided by third-party developers
110and reused by client application developers. Please see the documentation
111about \l{QML Modules}{QML modules} for more information about how to create and
112deploy a QML extension module.
113
114For more information about the different C++ and QML integration methods, see
115the \l{Overview - QML and C++ Integration}{C++ and QML integration overview}
116page.
117*/