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