source: trunk/doc/src/objectmodel/properties.qdoc@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 12.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at qt-info@nokia.com.
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \page properties.html
30 \title The Property System
31 \brief An overview of Qt's property system.
32
33 \ingroup qt-basic-concepts
34 \target Qt's Property System
35
36 Qt provides a sophisticated property system similar to the ones
37 supplied by some compiler vendors. However, as a compiler- and
38 platform-independent library, Qt does not rely on non-standard
39 compiler features like \c __property or \c [property]. The Qt
40 solution works with \e any standard C++ compiler on every platform
41 Qt supports. It is based on the \l {Meta-Object System} that also
42 provides inter-object communication via \l{signals and slots}.
43
44 \section1 Requirements for Declaring Properties
45
46 To declare a property, use the \l {Q_PROPERTY()} {Q_PROPERTY()}
47 macro in a class that inherits QObject.
48
49 \snippet doc/src/snippets/code/doc_src_properties.qdoc 0
50
51 Here are some typical examples of property declarations taken from
52 class QWidget.
53
54 \snippet doc/src/snippets/code/doc_src_properties.qdoc 1
55
56 A property behaves like a class data member, but it has additional
57 features accessible through the \l {Meta-Object System}.
58
59 \list
60
61 \o A \c READ accessor function is required. It is for reading the
62 property value. Ideally, a const function is used for this purpose,
63 and it must return either the property's type or a pointer or
64 reference to that type. e.g., QWidget::focus is a read-only property
65 with \c READ function, QWidget::hasFocus().
66
67 \o A \c WRITE accessor function is optional. It is for setting the
68 property value. It must return void and must take exactly one
69 argument, either of the property's type or a pointer or reference
70 to that type. e.g., QWidget::enabled has the \c WRITE function
71 QWidget::setEnabled(). Read-only properties do not need \c WRITE
72 functions. e.g., QWidget::focus has no \c WRITE function.
73
74 \o A \c RESET function is optional. It is for setting the property
75 back to its context specific default value. e.g., QWidget::cursor
76 has the typical \c READ and \c WRITE functions, QWidget::cursor()
77 and QWidget::setCursor(), and it also has a \c RESET function,
78 QWidget::unsetCursor(), since no call to QWidget::setCursor() can
79 mean \e {reset to the context specific cursor}. The \c RESET
80 function must return void and take no parameters.
81
82 \o A \c NOTIFY signal is optional. If defined, it should specify one
83 existing signal in that class that is emitted whenever the value
84 of the property changes.
85
86 \o The \c DESIGNABLE attribute indicates whether the property
87 should be visible in the property editor of GUI design tool (e.g.,
88 \l {Qt Designer}). Most properties are \c DESIGNABLE (default
89 true). Instead of true or false, you can specify a boolean
90 member function.
91
92 \o The \c SCRIPTABLE attribute indicates whether this property
93 should be accessible by a scripting engine (default true).
94 Instead of true or false, you can specify a boolean member
95 function.
96
97 \o The \c STORED attribute indicates whether the property should
98 be thought of as existing on its own or as depending on other
99 values. It also indicates whether the property value must be saved
100 when storing the object's state. Most properties are \c STORED
101 (default true), but e.g., QWidget::minimumWidth() has \c STORED
102 false, because its value is just taken from the width component
103 of property QWidget::minimumSize(), which is a QSize.
104
105 \o The \c USER attribute indicates whether the property is
106 designated as the user-facing or user-editable property for the
107 class. Normally, there is only one \c USER property per class
108 (default false). e.g., QAbstractButton::checked is the user
109 editable property for (checkable) buttons. Note that QItemDelegate
110 gets and sets a widget's \c USER property.
111
112 \o The presence of the \c CONSTANT attibute indicates that the property
113 value is constant. For a given object instance, the READ method of a
114 constant property must return the same value every time it is called. This
115 constant value may be different for different instances of the object. A
116 constant property cannot have a WRITE method or a NOTIFY signal.
117
118 \o The presence of the \c FINAL attribute indicates that the property
119 will not be overridden by a derived class. This can be used for performance
120 optimizations in some cases, but is not enforced by moc. Care must be taken
121 never to override a \c FINAL property.
122
123 \endlist
124
125 The \c READ, \c WRITE, and \c RESET functions can be inherited.
126 They can also be virtual. When they are inherited in classes where
127 multiple inheritance is used, they must come from the first
128 inherited class.
129
130 The property type can be any type supported by QVariant, or it can
131 be a user-defined type. In this example, class QDate is considered
132 to be a user-defined type.
133
134 \snippet doc/src/snippets/code/doc_src_properties.qdoc 2
135
136 Because QDate is user-defined, you must include the \c{<QDate>}
137 header file with the property declaration.
138
139 For QMap, QList, and QValueList properties, the property value is
140 a QVariant whose value is the entire list or map. Note that the
141 Q_PROPERTY string cannot contain commas, because commas separate
142 macro arguments. Therefore, you must use \c QMap as the property
143 type instead of \c QMap<QString,QVariant>. For consistency, also
144 use \c QList and \c QValueList instead of \c QList<QVariant> and
145 \c QValueList<QVariant>.
146
147 \section1 Reading and Writing Properties with the Meta-Object System
148
149 A property can be read and written using the generic functions
150 QObject::property() and QObject::setProperty(), without knowing
151 anything about the owning class except the property's name. In
152 the code snippet below, the call to QAbstractButton::setDown() and
153 the call to QObject::setProperty() both set property "down".
154
155 \snippet doc/src/snippets/code/doc_src_properties.qdoc 3
156
157 Accessing a property through its \c WRITE accessor is the better
158 of the two, because it is faster and gives better diagnostics at
159 compile time, but setting the property this way requires that you
160 know about the class at compile time. Accessing properties by name
161 lets you access classes you don't know about at compile time. You
162 can \e discover a class's properties at run time by querying its
163 QObject, QMetaObject, and \l {QMetaProperty} {QMetaProperties}.
164
165 \snippet doc/src/snippets/code/doc_src_properties.qdoc 4
166
167 In the above snippet, QMetaObject::property() is used to get \l
168 {QMetaProperty} {metadata} about each property defined in some
169 unknown class. The property name is fetched from the metadata and
170 passed to QObject::property() to get the \l {QVariant} {value} of
171 the property in the current \l {QObject}{object}.
172
173 \section1 A Simple Example
174
175 Suppose we have a class MyClass, which is derived from QObject and
176 which uses the Q_OBJECT macro in its private section. We want to
177 declare a property in MyClass to keep track of a priorty
178 value. The name of the property will be \e priority, and its type
179 will be an enumeration type named \e Priority, which is defined in
180 MyClass.
181
182 We declare the property with the Q_PROPERTY() macro in the private
183 section of the class. The required \c READ function is named \c
184 priority, and we include a \c WRITE function named \c setPriority.
185 The enumeration type must be registered with the \l {Meta-Object
186 System} using the Q_ENUMS() macro. Registering an enumeration type
187 makes the enumerator names available for use in calls to
188 QObject::setProperty(). We must also provide our own declarations
189 for the \c READ and \c WRITE functions. The declaration of MyClass
190 then might look like this:
191
192 \snippet doc/src/snippets/code/doc_src_properties.qdoc 5
193
194 The \c READ function is const and returns the property type. The
195 \c WRITE function returns void and has exactly one parameter of
196 the property type. The meta-object compiler enforces these
197 requirements.
198
199 Given a pointer to an instance of MyClass or a pointer to a
200 QObject that is an instance of MyClass, we have two ways to set
201 its priority property:
202
203 \snippet doc/src/snippets/code/doc_src_properties.qdoc 6
204
205 In the example, the enumeration type that is the property type is
206 declared in MyClass and registered with the \l{Meta-Object System}
207 using the Q_ENUMS() macro. This makes the enumeration values
208 available as strings for use as in the call to setProperty(). Had
209 the enumeration type been declared in another class, its fully
210 qualified name (i.e., OtherClass::Priority) would be required, and
211 that other class would also have to inherit QObject and register
212 the enumeration type there using the Q_ENUMS() macro.
213
214 A similar macro, Q_FLAGS(), is also available. Like Q_ENUMS(), it
215 registers an enumeration type, but it marks the type as being a
216 set of \e flags, i.e. values that can be OR'd together. An I/O
217 class might have enumeration values \c Read and \c Write and then
218 QObject::setProperty() could accept \c{Read | Write}. Q_FLAGS()
219 should be used to register this enumeration type.
220
221 \section1 Dynamic Properties
222
223 QObject::setProperty() can also be used to add \e new properties
224 to an instance of a class at runtime. When it is called with a
225 name and a value, if a property with the given name exists in the
226 QObject, and if the given value is compatible with the property's
227 type, the value is stored in the property, and true is returned.
228 If the value is \e not compatible with the property's type, the
229 property is \e not changed, and false is returned. But if the
230 property with the given name doesn't exist in the QObject (i.e.,
231 if it wasn't declared with Q_PROPERTY(), a new property with the
232 given name and value is automatically added to the QObject, but
233 false is still returned. This means that a return of false can't
234 be used to determine whether a particular property was actually
235 set, unless you know in advance that the property already exists
236 in the QObject.
237
238 Note that \e dynamic properties are added on a per instance basis,
239 i.e., they are added to QObject, not QMetaObject. A property can
240 be removed from an instance by passing the property name and an
241 invalid QVariant value to QObject::setProperty(). The default
242 constructor for QVariant constructs an invalid QVariant.
243
244 Dynamic properties can be queried with QObject::property(), just
245 like properties declared at compile time with Q_PROPERTY().
246
247 \sa {Meta-Object System}, {Signals and Slots}
248
249 \section1 Properties and Custom Types
250
251 Custom types used by properties need to be registered using the
252 Q_DECLARE_METATYPE() macro so that their values can be stored in
253 QVariant objects. This makes them suitable for use with both
254 static properties declared using the Q_PROPERTY() macro in class
255 definitions and dynamic properties created at run-time.
256
257 \sa Q_DECLARE_METATYPE(), QMetaType, QVariant
258
259 \section1 Adding Additional Information to a Class
260
261 Connected to the property system is an additional macro,
262 Q_CLASSINFO(), that can be used to attach additional
263 \e{name}--\e{value} pairs to a class's meta-object, for example:
264
265 \snippet doc/src/snippets/code/doc_src_properties.qdoc 7
266
267 Like other meta-data, class information is accessible at run-time
268 through the meta-object; see QMetaObject::classInfo() for details.
269*/
Note: See TracBrowser for help on using the repository browser.