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 The presence of the \c FINAL attribute indicates that the property
132
will not be overridden by a derived class. This can be used for performance
133
optimizations in some cases, but is not enforced by moc. Care must be taken
134
never to override a \c FINAL property.
135
136
\li The presence of the \c REQUIRED attribute indicates that the property
137
should be set by a user of the class. This is not enforced by moc, and is
138
mostly useful for classes exposed to QML. In QML, classes with REQUIRED
139
properties cannot be instantiated unless all REQUIRED properties have
140
been set.
141
142
\endlist
143
144
The \c READ, \c WRITE, and \c RESET functions can be inherited.
145
They can also be virtual. When they are inherited in classes where
146
multiple inheritance is used, they must come from the first
147
inherited class.
148
149
The property type can be any type supported by QVariant, or it can
150
be a user-defined type. In this example, class QDate is considered
151
to be a user-defined type.
152
153
\snippet code/doc_src_properties.cpp 2
154
155
Because QDate is user-defined, you must include the \c{<QDate>}
156
header file with the property declaration.
157
158
For historical reasons, \a QMap and \a QList as property types
159
are synonym of \a QVariantMap and \a QVariantList.
160
161
\section1 Reading and Writing Properties with the Meta-Object System
162
163
A property can be read and written using the generic functions
164
QObject::property() and QObject::setProperty(), without knowing
165
anything about the owning class except the property's name. In
166
the code snippet below, the call to QAbstractButton::setDown() and
167
the call to QObject::setProperty() both set property "down".
168
169
\snippet code/doc_src_properties.cpp 3
170
171
Accessing a property through its \c WRITE accessor is the better
172
of the two, because it is faster and gives better diagnostics at
173
compile time, but setting the property this way requires that you
174
know about the class at compile time. Accessing properties by name
175
lets you access classes you don't know about at compile time. You
176
can \e discover a class's properties at run time by querying its
177
QObject, QMetaObject, and \l {QMetaProperty} {QMetaProperties}.
178
179
\snippet code/doc_src_properties.cpp 4
180
181
In the above snippet, QMetaObject::property() is used to get \l
182
{QMetaProperty} {metadata} about each property defined in some
183
unknown class. The property name is fetched from the metadata and
184
passed to QObject::property() to get the \l {QVariant} {value} of
185
the property in the current \l {QObject}{object}.
186
187
\section1 A Simple Example
188
189
Suppose we have a class \c MyClass, which is derived from QObject and which
190
uses the Q_OBJECT macro. We want to declare a property in \c MyClass to keep
191
track of a priority value. The name of the property will be \c priority, and
192
its type will be an enumeration type named \c Priority, which is defined in
193
\c MyClass.
194
195
We declare the property with the Q_PROPERTY() macro in the private
196
section of the class. The required \c READ function is named \c
197
priority, and we include a \c WRITE function named \c setPriority.
198
The enumeration type must be registered with the \l {Meta-Object
199
System} using the Q_ENUM() macro. Registering an enumeration type
200
makes the enumerator names available for use in calls to
201
QObject::setProperty(). We must also provide our own declarations
202
for the \c READ and \c WRITE functions. The declaration of \c MyClass
203
then might look like this:
204
205
\snippet code/doc_src_properties.cpp 5
206
207
The \c READ function is const and returns the property type. The
208
\c WRITE function returns void and has exactly one parameter of
209
the property type. The meta-object compiler enforces these
210
requirements. The equality check in the \c WRITE function, while not
211
mandatory, is good practice as there is no point in notifying and
212
potentially forcing re-evaluation in other places if nothing has
213
changed.
214
215
Given a pointer to an instance of \c MyClass or a pointer to a
216
QObject that is an instance of \c MyClass, we have two ways to set
217
its priority property:
218
219
\snippet code/doc_src_properties.cpp 6
220
221
In the example, the enumeration type that is the property type is
222
declared in \c MyClass and registered with the \l{Meta-Object System}
223
using the Q_ENUM() macro. This makes the enumeration values
224
available as strings for use as in the call to \l{QObject::}{setProperty()}.
225
Had the enumeration type been declared in another class, its fully
226
qualified name (i.e., OtherClass::Priority) would be required, and
227
that other class would also have to inherit QObject and register
228
the enumeration type there using the Q_ENUM() macro.
229
230
A similar macro, Q_FLAG(), is also available. Like Q_ENUM(), it
231
registers an enumeration type, but it marks the type as being a
232
set of \e flags, i.e., values that can be OR'd together. An I/O
233
class might have enumeration values \c Read and \c Write and then
234
QObject::setProperty() could accept \c{Read | Write}. Q_FLAG()
235
should be used to register this enumeration type.
236
237
\section1 Dynamic Properties
238
239
QObject::setProperty() can also be used to add \e new properties
240
to an instance of a class at runtime. When it is called with a
241
name and a value, if a property with the given name exists in the
242
QObject, and if the given value is compatible with the property's
243
type, the value is stored in the property, and true is returned.
244
If the value is \e not compatible with the property's type, the
245
property is \e not changed, and false is returned. But if the
246
property with the given name doesn't exist in the QObject (i.e.,
247
if it wasn't declared with Q_PROPERTY()), a new property with the
248
given name and value is automatically added to the QObject, but
249
false is still returned. This means that a return of false can't
250
be used to determine whether a particular property was actually
251
set, unless you know in advance that the property already exists
252
in the QObject.
253
254
Note that \e dynamic properties are added on a per instance basis,
255
i.e., they are added to QObject, not QMetaObject. A property can
256
be removed from an instance by passing the property name and an
257
invalid QVariant value to QObject::setProperty(). The default
258
constructor for QVariant constructs an invalid QVariant.
259
260
Dynamic properties can be queried with QObject::property(), just
261
like properties declared at compile time with Q_PROPERTY().
262
263
\sa {Meta-Object System}, {Signals and Slots}
264
265
\section1 Properties and Custom Types
266
267
Custom types used by properties need to be registered using the
268
Q_DECLARE_METATYPE() macro so that their values can be stored in
269
QVariant objects. This makes them suitable for use with both
270
static properties declared using the Q_PROPERTY() macro in class
271
definitions and dynamic properties created at run-time.
272
273
\sa Q_DECLARE_METATYPE(), QMetaType, QVariant
274
275
\section1 Adding Additional Information to a Class
276
277
Connected to the property system is an additional macro,
278
Q_CLASSINFO(), that can be used to attach additional
279
\e{name}--\e{value} pairs to a class's meta-object. This is
280
used for instance to mark a property as the \e default one
281
in the context of \l{QML Object Types}:
282
283
\snippet code/doc_src_properties.cpp 7
284
285
Like other meta-data, class information is accessible at run-time
286
through the meta-object; see QMetaObject::classInfo() for details.
287
288
\section1 Using Bindable Properties
289
290
Three different types can be used to implement bindable properties:
291
\list
292
\li \l QProperty
293
\li \l QObjectBindableProperty
294
\li \l QObjectComputedProperty.
295
\endlist
296
The first one is a general class for bindable properties. The latter
297
two can only be used inside a \l QObject.
298
299
For more information, including examples, see the classes mentioned above
300
and the general tips on implementing and using
301
\l {Qt Bindable Properties}{bindable properties}.
302
303
\sa {Qt Bindable Properties}, {Defining QML Types from C++}
304
*/
qtbase
src
corelib
doc
src
objectmodel
properties.qdoc
Generated on
for Qt by
1.14.0