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
properties.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page properties.html
6
\title The Property System
7
\brief An overview of Qt's property system.
8
\ingroup explanations-basics
9
\ingroup qt-basic-concepts
10
\keyword Qt's Property System
11
12
Qt provides a sophisticated property system similar to the ones
13
supplied by some compiler vendors. However, as a compiler- and
14
platform-independent library, Qt does not rely on non-standard
15
compiler features like \c __property or \c [property]. The Qt
16
solution works with \e any standard C++ compiler on every platform
17
Qt supports. It is based on the \l {Meta-Object System} that also
18
provides inter-object communication via \l{signals and slots}.
19
20
\section1 Requirements for Declaring Properties
21
22
To declare a property, use the \l {Q_PROPERTY()} {Q_PROPERTY()}
23
macro in a class that inherits QObject.
24
25
\snippet code/doc_src_properties.cpp 0
26
27
Here are some typical examples of property declarations taken from
28
class QWidget.
29
30
\snippet code/doc_src_properties.cpp 1
31
32
Here is an example showing how to export member variables as Qt
33
properties using the \c MEMBER keyword.
34
Note that a \c NOTIFY signal must be specified to allow QML property bindings.
35
36
\snippet code/doc_src_properties.cpp 8
37
38
A property behaves like a class data member, but it has additional
39
features accessible through the \l {Meta-Object System}.
40
41
\list
42
43
\li A \c READ accessor function is required if no \c MEMBER variable was
44
specified. It is for reading the property value. Ideally, a const function
45
is used for this purpose, and it must return either the property's type or a
46
const reference to that type. e.g., QWidget::focus is a read-only
47
property with \c READ function, QWidget::hasFocus(). If a \c BINDABLE is
48
specified, you can write \c{READ default} to have the \c READ accessor
49
generated from the \c BINDABLE.
50
51
\li A \c WRITE accessor function is optional. It is for setting the
52
property value. It must return void and must take exactly one
53
argument, either of the property's type or a pointer or reference
54
to that type. e.g., QWidget::enabled has the \c WRITE function
55
QWidget::setEnabled(). Read-only properties do not need \c WRITE
56
functions. e.g., QWidget::focus has no \c WRITE function. If you specify
57
both a \c BINDABLE and \c{WRITE default}, a \c WRITE accessor will be
58
generated from the \c BINDABLE. The generated \c WRITE accessor will \e not
59
explicitly emit any signal declared with \c NOTIFY. You should register
60
the signal as change handler to the \c BINDABLE, for example using
61
\l{Q_OBJECT_BINDABLE_PROPERTY}.
62
63
\li A \c MEMBER variable association is required if no \c READ accessor
64
function is specified. This makes the given member variable
65
readable and writable without the need of creating \c READ and \c WRITE accessor
66
functions. It's still possible to use \c READ or \c WRITE accessor functions in
67
addition to \c MEMBER variable association (but not both), if you need to
68
control the variable access.
69
70
\li A \c RESET function is optional. It is for setting the property
71
back to its context specific default value. e.g., QWidget::cursor
72
has the typical \c READ and \c WRITE functions, QWidget::cursor()
73
and QWidget::setCursor(), and it also has a \c RESET function,
74
QWidget::unsetCursor(), since no call to QWidget::setCursor() can
75
mean \e {reset to the context specific cursor}. The \c RESET
76
function must return void and take no parameters.
77
78
\li A \c NOTIFY signal is optional. If defined, it should specify one
79
existing signal in that class that is emitted whenever the value
80
of the property changes.
81
\c NOTIFY signals for \c MEMBER variables must take zero or one parameter,
82
which must be of the same type as the property. The parameter will take the
83
new value of the property. The \c NOTIFY signal should only be emitted when
84
the property has really been changed, to avoid bindings being unnecessarily
85
re-evaluated in QML, for example. The signal is emitted automatically when
86
the property is changed via the Qt API (QObject::setProperty,
87
QMetaProperty, etc.), but not when the MEMBER is changed directly.
88
89
\li A \c REVISION number or \c REVISION() macro is optional. If included,
90
it defines the property and its notifier signal to be used in a particular
91
revision of the API (usually for exposure to QML). If not included, it
92
defaults to 0.
93
94
\li The \c DESIGNABLE attribute indicates whether the property
95
should be visible in the property editor of GUI design tool (e.g.,
96
\l {Qt Widgets Designer Manual}{\QD}). Most properties are \c DESIGNABLE
97
(default true). Valid values are true and false.
98
99
\li The \c SCRIPTABLE attribute indicates whether this property
100
should be accessible by a scripting engine (default true).
101
Valid values are true and false.
102
103
\li The \c STORED attribute indicates whether the property should
104
be thought of as existing on its own or as depending on other
105
values. It also indicates whether the property value must be saved
106
when storing the object's state. Most properties are \c STORED
107
(default true), but e.g., QWidget::minimumWidth() has \c STORED
108
false, because its value is just taken from the width component
109
of property QWidget::minimumSize(), which is a QSize.
110
111
\li The \c USER attribute indicates whether the property is
112
designated as the user-facing or user-editable property for the
113
class. Normally, there is only one \c USER property per class
114
(default false). e.g., QAbstractButton::checked is the user
115
editable property for (checkable) buttons. Note that QItemDelegate
116
gets and sets a widget's \c USER property.
117
118
\li The \c {BINDABLE bindableProperty} attribute indicates that the
119
property supports \l {Qt Bindable Properties}{bindings},
120
and that it is possible to set and inspect
121
bindings to this property via the meta object system (QMetaProperty).
122
\c bindableProperty names a class member of type QBindable<T>, where T
123
is the property type. This attribute was introduced in Qt 6.0.
124
125
\li The presence of the \c CONSTANT attribute indicates that the property
126
value is constant. For a given object instance, the READ method of a
127
constant property must return the same value every time it is called. This
128
constant value may be different for different instances of the object. A
129
constant property cannot have a WRITE method or a NOTIFY signal.
130
131
\li \c FINAL, \c VIRTUAL, \c OVERRIDE modifiers mirror the semantics of their C++ and
132
\l {Override Semantics}{QML counterparts}, allowing to make property overriding explicit at the
133
meta-object level.
134
135
\note At present, these modifiers are not enforced by moc.
136
They are recognized syntactically and are primarily used for QML runtime enforcement and tooling
137
diagnostics. Future versions may introduce stricter compile-time validation and warnings for
138
invalid overrides across modules.
139
140
\note If you want to change accessing behaviour for a property, use the
141
polymorphism provided by C++.
142
143
144
\li The presence of the \c REQUIRED attribute indicates that the property
145
should be set by a user of the class. This is not enforced by moc, and is
146
mostly useful for classes exposed to QML. In QML, classes with REQUIRED
147
properties cannot be instantiated unless all REQUIRED properties have
148
been set.
149
150
\endlist
151
152
The \c READ, \c WRITE, and \c RESET functions can be inherited.
153
They can also be virtual. When they are inherited in classes where
154
multiple inheritance is used, they must come from the first
155
inherited class.
156
157
The property type can be any type supported by QVariant, or it can
158
be a user-defined type. In this example, class QDate is considered
159
to be a user-defined type.
160
161
\snippet code/doc_src_properties.cpp 2
162
163
Because QDate is user-defined, you must include the \c{<QDate>}
164
header file with the property declaration.
165
166
For historical reasons, \a QMap and \a QList as property types
167
are synonym of \a QVariantMap and \a QVariantList.
168
169
\section1 Reading and Writing Properties with the Meta-Object System
170
171
A property can be read and written using the generic functions
172
QObject::property() and QObject::setProperty(), without knowing
173
anything about the owning class except the property's name. In
174
the code snippet below, the call to QAbstractButton::setDown() and
175
the call to QObject::setProperty() both set property "down".
176
177
\snippet code/doc_src_properties.cpp 3
178
179
Accessing a property through its \c WRITE accessor is the better
180
of the two, because it is faster and gives better diagnostics at
181
compile time, but setting the property this way requires that you
182
know about the class at compile time. Accessing properties by name
183
lets you access classes you don't know about at compile time. You
184
can \e discover a class's properties at run time by querying its
185
QObject, QMetaObject, and \l {QMetaProperty} {QMetaProperties}.
186
187
\snippet code/doc_src_properties.cpp 4
188
189
In the above snippet, QMetaObject::property() is used to get \l
190
{QMetaProperty} {metadata} about each property defined in some
191
unknown class. The property name is fetched from the metadata and
192
passed to QObject::property() to get the \l {QVariant} {value} of
193
the property in the current \l {QObject}{object}.
194
195
\section1 A Simple Example
196
197
Suppose we have a class \c MyClass, which is derived from QObject and which
198
uses the Q_OBJECT macro. We want to declare a property in \c MyClass to keep
199
track of a priority value. The name of the property will be \c priority, and
200
its type will be an enumeration type named \c Priority, which is defined in
201
\c MyClass.
202
203
We declare the property with the Q_PROPERTY() macro in the private
204
section of the class. The required \c READ function is named \c
205
priority, and we include a \c WRITE function named \c setPriority.
206
The enumeration type must be registered with the \l {Meta-Object
207
System} using the Q_ENUM() macro. Registering an enumeration type
208
makes the enumerator names available for use in calls to
209
QObject::setProperty(). We must also provide our own declarations
210
for the \c READ and \c WRITE functions. The declaration of \c MyClass
211
then might look like this:
212
213
\snippet code/doc_src_properties.cpp 5
214
215
The \c READ function is const and returns the property type. The
216
\c WRITE function returns void and has exactly one parameter of
217
the property type. The meta-object compiler enforces these
218
requirements. The equality check in the \c WRITE function, while not
219
mandatory, is good practice as there is no point in notifying and
220
potentially forcing re-evaluation in other places if nothing has
221
changed.
222
223
Given a pointer to an instance of \c MyClass or a pointer to a
224
QObject that is an instance of \c MyClass, we have two ways to set
225
its priority property:
226
227
\snippet code/doc_src_properties.cpp 6
228
229
In the example, the enumeration type that is the property type is
230
declared in \c MyClass and registered with the \l{Meta-Object System}
231
using the Q_ENUM() macro. This makes the enumeration values
232
available as strings for use as in the call to \l{QObject::}{setProperty()}.
233
Had the enumeration type been declared in another class, its fully
234
qualified name (i.e., OtherClass::Priority) would be required, and
235
that other class would also have to inherit QObject and register
236
the enumeration type there using the Q_ENUM() macro.
237
238
A similar macro, Q_FLAG(), is also available. Like Q_ENUM(), it
239
registers an enumeration type, but it marks the type as being a
240
set of \e flags, i.e., values that can be OR'd together. An I/O
241
class might have enumeration values \c Read and \c Write and then
242
QObject::setProperty() could accept \c{Read | Write}. Q_FLAG()
243
should be used to register this enumeration type.
244
245
\section1 Dynamic Properties
246
247
QObject::setProperty() can also be used to add \e new properties
248
to an instance of a class at runtime. When it is called with a
249
name and a value, if a property with the given name exists in the
250
QObject, and if the given value is compatible with the property's
251
type, the value is stored in the property, and true is returned.
252
If the value is \e not compatible with the property's type, the
253
property is \e not changed, and false is returned. But if the
254
property with the given name doesn't exist in the QObject (i.e.,
255
if it wasn't declared with Q_PROPERTY()), a new property with the
256
given name and value is automatically added to the QObject, but
257
false is still returned. This means that a return of false can't
258
be used to determine whether a particular property was actually
259
set, unless you know in advance that the property already exists
260
in the QObject.
261
262
Note that \e dynamic properties are added on a per instance basis,
263
i.e., they are added to QObject, not QMetaObject. A property can
264
be removed from an instance by passing the property name and an
265
invalid QVariant value to QObject::setProperty(). The default
266
constructor for QVariant constructs an invalid QVariant.
267
268
Dynamic properties can be queried with QObject::property(), just
269
like properties declared at compile time with Q_PROPERTY().
270
271
\sa {Meta-Object System}, {Signals and Slots}
272
273
\section1 Properties and Custom Types
274
275
Custom types used by properties need to be registered using the
276
Q_DECLARE_METATYPE() macro so that their values can be stored in
277
QVariant objects. This makes them suitable for use with both
278
static properties declared using the Q_PROPERTY() macro in class
279
definitions and dynamic properties created at run-time.
280
281
\sa Q_DECLARE_METATYPE(), QMetaType, QVariant
282
283
\section1 Adding Additional Information to a Class
284
285
Connected to the property system is an additional macro,
286
Q_CLASSINFO(), that can be used to attach additional
287
\e{name}--\e{value} pairs to a class's meta-object. This is
288
used for instance to mark a property as the \e default one
289
in the context of \l{QML Object Types}:
290
291
\snippet code/doc_src_properties.cpp 7
292
293
Like other meta-data, class information is accessible at run-time
294
through the meta-object; see QMetaObject::classInfo() for details.
295
296
\section1 Using Bindable Properties
297
298
Three different types can be used to implement bindable properties:
299
\list
300
\li \l QProperty
301
\li \l QObjectBindableProperty
302
\li \l QObjectComputedProperty.
303
\endlist
304
The first one is a general class for bindable properties. The latter
305
two can only be used inside a \l QObject.
306
307
For more information, including examples, see the classes mentioned above
308
and the general tips on implementing and using
309
\l {Qt Bindable Properties}{bindable properties}.
310
311
\sa {Qt Bindable Properties}, {Defining QML Types from C++}
312
*/
qtbase
src
corelib
doc
src
objectmodel
properties.qdoc
Generated on
for Qt by
1.16.1