Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 MyClass, which is derived from QObject and
190 which uses the Q_OBJECT macro in its private section. We want to
191 declare a property in MyClass to keep track of a priority
192 value. The name of the property will be \e priority, and its type
193 will be an enumeration type named \e Priority, which is defined in
194 MyClass.
195
196 We declare the property with the Q_PROPERTY() macro in the private
197 section of the class. The required \c READ function is named \c
198 priority, and we include a \c WRITE function named \c setPriority.
199 The enumeration type must be registered with the \l {Meta-Object
200 System} using the Q_ENUM() macro. Registering an enumeration type
201 makes the enumerator names available for use in calls to
202 QObject::setProperty(). We must also provide our own declarations
203 for the \c READ and \c WRITE functions. The declaration of MyClass
204 then might look like this:
205
206 \snippet code/doc_src_properties.cpp 5
207
208 The \c READ function is const and returns the property type. The
209 \c WRITE function returns void and has exactly one parameter of
210 the property type. The meta-object compiler enforces these
211 requirements. The equality check in the \c WRITE function, while not
212 mandatory, is good practice as there is no point in notifying and
213 potentially forcing re-evaluation in other places if nothing has
214 changed.
215
216 Given a pointer to an instance of MyClass or a pointer to a
217 QObject that is an instance of MyClass, we have two ways to set
218 its priority property:
219
220 \snippet code/doc_src_properties.cpp 6
221
222 In the example, the enumeration type that is the property type is
223 declared in MyClass and registered with the \l{Meta-Object System}
224 using the Q_ENUM() macro. This makes the enumeration values
225 available as strings for use as in the call to \l{QObject::}{setProperty()}. Had
226 the enumeration type been declared in another class, its fully
227 qualified name (i.e., OtherClass::Priority) would be required, and
228 that other class would also have to inherit QObject and register
229 the enumeration type there using the Q_ENUM() macro.
230
231 A similar macro, Q_FLAG(), is also available. Like Q_ENUM(), it
232 registers an enumeration type, but it marks the type as being a
233 set of \e flags, i.e. values that can be OR'd together. An I/O
234 class might have enumeration values \c Read and \c Write and then
235 QObject::setProperty() could accept \c{Read | Write}. Q_FLAG()
236 should be used to register this enumeration type.
237
238 \section1 Dynamic Properties
239
240 QObject::setProperty() can also be used to add \e new properties
241 to an instance of a class at runtime. When it is called with a
242 name and a value, if a property with the given name exists in the
243 QObject, and if the given value is compatible with the property's
244 type, the value is stored in the property, and true is returned.
245 If the value is \e not compatible with the property's type, the
246 property is \e not changed, and false is returned. But if the
247 property with the given name doesn't exist in the QObject (i.e.,
248 if it wasn't declared with Q_PROPERTY()), a new property with the
249 given name and value is automatically added to the QObject, but
250 false is still returned. This means that a return of false can't
251 be used to determine whether a particular property was actually
252 set, unless you know in advance that the property already exists
253 in the QObject.
254
255 Note that \e dynamic properties are added on a per instance basis,
256 i.e., they are added to QObject, not QMetaObject. A property can
257 be removed from an instance by passing the property name and an
258 invalid QVariant value to QObject::setProperty(). The default
259 constructor for QVariant constructs an invalid QVariant.
260
261 Dynamic properties can be queried with QObject::property(), just
262 like properties declared at compile time with Q_PROPERTY().
263
264 \sa {Meta-Object System}, {Signals and Slots}
265
266 \section1 Properties and Custom Types
267
268 Custom types used by properties need to be registered using the
269 Q_DECLARE_METATYPE() macro so that their values can be stored in
270 QVariant objects. This makes them suitable for use with both
271 static properties declared using the Q_PROPERTY() macro in class
272 definitions and dynamic properties created at run-time.
273
274 \sa Q_DECLARE_METATYPE(), QMetaType, QVariant
275
276 \section1 Adding Additional Information to a Class
277
278 Connected to the property system is an additional macro,
279 Q_CLASSINFO(), that can be used to attach additional
280 \e{name}--\e{value} pairs to a class's meta-object. This is
281 used for instance to mark a property as the \e default one
282 in the context of \l{QML Object Types}:
283
284 \snippet code/doc_src_properties.cpp 7
285
286 Like other meta-data, class information is accessible at run-time
287 through the meta-object; see QMetaObject::classInfo() for details.
288
289 \section1 Using Bindable Properties
290
291 Three different types can be used to implement bindable properties:
292 \list
293 \li \l QProperty
294 \li \l QObjectBindableProperty
295 \li \l QObjectComputedProperty.
296 \endlist
297 The first one is a general class for bindable properties. The latter
298 two can only be used inside a \l QObject.
299
300 For more information, including examples, see the classes mentioned above
301 and the general tips on implementing and using
302 \l {Qt Bindable Properties}{bindable properties}.
303
304 \sa {Qt Bindable Properties}, {Defining QML Types from C++}
305*/