source: trunk/doc/html/properties.html@ 203

Last change on this file since 203 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 11.3 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/object.doc:210 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Properties</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Properties</h1>
33
34
35
36<p> Qt provides a sophisticated property system similar to those supplied
37by some compiler vendors. However, as a compiler- and
38platform-independent library, Qt cannot rely on non-standard compiler
39features like <tt>__property</tt> or <tt>[property]</tt>. Our solution works with
40<em>any</em> standard C++ compiler on every platform we support. It's based
41on the meta-object system that also provides object communication
42through <a href="signalsandslots.html">signals and slots</a>.
43<p> The <tt>Q_PROPERTY</tt> macro in a class declaration declares a
44property. Properties can only be declared in classes that inherit <a href="qobject.html">QObject</a>. A second macro, <tt>Q_OVERRIDE</tt>, can be used to override some
45aspects of an inherited property in a subclass. (See <a href="#override">Q_OVERRIDE</a>.)
46<p> To the outer world, a property appears to be similar to a data member.
47But properties have several features that distinguish them from
48ordinary data members:
49<p> <ul>
50<li> A read function. This always exists.
51<p> <li> A write function. This is optional: read-only properties like <a href="qwidget.html#isDesktop">QWidget::isDesktop</a>() do not have one.
52<p> <li> An attribute "stored" that indicates persistence. Most properties
53are stored, but a few virtual properties are not. For example, <a href="qwidget.html#minimumWidth">QWidget::minimumWidth</a>() isn't stored, since it's just a view of
54<a href="qwidget.html#minimumSize">QWidget::minimumSize</a>(), and has no data of its own.
55<p> <li> A reset function to set a property back to its context specific
56default value. This is very rare, but for example, <a href="qwidget.html#font">QWidget::font</a>()
57needs this, since no call to <a href="qwidget.html#setFont">QWidget::setFont</a>() can mean 'reset to
58the context specific font'.
59<p> <li> An attribute "designable" that indicates whether it makes sense to
60make the property available in a GUI builder (e.g. <a href="designer-manual.html">Qt Designer</a>). For most properties this
61makes sense, but not for all, e.g. <a href="qbutton.html#isDown">QButton::isDown</a>(). The user can
62press buttons, and the application programmer can make the program
63press its own buttons, but a GUI design tool can't press buttons.
64<p> </ul>
65<p> The read, write, and reset functions must be public member functions
66from the class in which the property is defined.
67<p> Properties can be read and written through generic functions in
68<a href="qobject.html">QObject</a> without knowing anything about the class in use. These two
69function calls are equivalent:
70<p> <pre>
71 // QButton *b and QObject *o point to the same button
72 b-&gt;setDown( TRUE );
73 o-&gt;setProperty( "down", TRUE );
74</pre>
75
76<p> Equivalent, that is, except that the first is faster, and provides
77much better diagnostics at compile time. When practical, the first is
78better. However, since you can get a list of all available properties
79for any QObject through its <a href="qmetaobject.html">QMetaObject</a>, <a href="qobject.html#setProperty">QObject::setProperty</a>()
80can give you control over classes that weren't available at compile
81time.
82<p> As well as <a href="qobject.html#setProperty">QObject::setProperty</a>(), there is a corresponding <a href="qobject.html#property">QObject::property</a>() function. <a href="qmetaobject.html#propertyNames">QMetaObject::propertyNames</a>() returns
83the names of all available properties. <a href="qmetaobject.html#property">QMetaObject::property</a>()
84returns the property data for a named property: a <a href="qmetaproperty.html">QMetaProperty</a>
85object.
86<p> Here's a simple example that shows the most important property
87functions in use:
88<p> <pre>
89 class MyClass : public <a href="qobject.html">QObject</a>
90 {
91 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
92 public:
93 MyClass( <a href="qobject.html">QObject</a> * parent=0, const char * name=0 );
94 ~MyClass();
95
96 enum Priority { High, Low, VeryHigh, VeryLow };
97 void setPriority( Priority );
98 Priority priority() const;
99 };
100</pre>
101
102<p> The class has a property "priority" that is not yet known to the <a href="metaobjects.html#meta-object">meta object</a> system. In order to make the property known, you must
103declare it with the <tt>Q_PROPERTY</tt> macro. The syntax is as follows:
104<p> <pre>
105Q_PROPERTY( type name READ getFunction [WRITE setFunction]
106 [RESET resetFunction] [DESIGNABLE bool]
107 [SCRIPTABLE bool] [STORED bool] )
108</pre>
109
110<p> For the declaration to be valid, the get function must be const and
111to return either the type itself, a pointer to it, or a reference to
112it. The optional write function must return void and must take exactly
113one argument, either the type itself, a pointer or a const reference
114to it. The meta object compiler enforces this.
115<p> The type of a property can be any <a href="qvariant.html">QVariant</a> supported type or an
116enumeration type declared in the class itself. Since <tt>MyClass</tt> uses
117the enumeration type <tt>Priority</tt> for the property, this type must be
118registered with the property system as well.
119<p> There are two exceptions to the above: The type of a property can also
120be either <a href="qvaluelist.html">QValueList&lt;QVariant&gt;</a> or <a href="qmap.html">QMap&lt;QString,QVariant&gt;</a>. In
121these cases the type must be specified as <a href="qvaluelist.html">QValueList</a> or as <a href="qmap.html">QMap</a>
122(i.e. without their template parameters).
123<p> It is possible to set a value by name, like this:
124<pre>
125 obj-&gt;setProperty( "priority", "VeryHigh" );
126</pre>
127
128In the case of <a href="qvaluelist.html">QValueList</a> and <a href="qmap.html">QMap</a> properties the value passes
129is a <a href="qvariant.html">QVariant</a> whose value is the entire list or map.
130<p> Enumeration types are registered with the <tt>Q_ENUMS</tt> macro. Here's the
131final class declaration including the property related declarations:
132<p> <pre>
133 class MyClass : public <a href="qobject.html">QObject</a>
134 {
135 Q_OBJECT
136 Q_PROPERTY( Priority priority READ priority WRITE setPriority )
137 Q_ENUMS( Priority )
138 public:
139 MyClass( <a href="qobject.html">QObject</a> * parent=0, const char * name=0 );
140 ~MyClass();
141
142 enum Priority { High, Low, VeryHigh, VeryLow };
143 void setPriority( Priority );
144 Priority priority() const;
145 };
146</pre>
147
148<p> Another similar macro is <tt>Q_SETS</tt>. Like <tt>Q_ENUMS</tt>, it registers an
149enumeration type but marks it in addition as a "set", i.e. the
150enumeration values can be OR-ed together. An I/O class might have
151enumeration values "Read" and "Write" and accept "Read|Write": such an
152enum is best handled with <tt>Q_SETS</tt>, rather than <tt>Q_ENUMS</tt>.
153<p> The remaining keywords in the <tt>Q_PROPERTY</tt> section are <tt>RESET</tt>, <tt>DESIGNABLE</tt>, <tt>SCRIPTABLE</tt> and <tt>STORED</tt>.
154<p> <tt>RESET</tt> names a function that will set the property to its default
155state (which may have changed since initialization). The function
156must return void and take no arguments.
157<p> <tt>DESIGNABLE</tt> declares whether this property is suitable for
158modification by a GUI design tool. The default is <tt>TRUE</tt> for
159writable properties; otherwise <tt>FALSE</tt>. Instead of <tt>TRUE</tt> or <tt>FALSE</tt>, you can specify a boolean member function.
160<p> <tt>SCRIPTABLE</tt> declares whether this property is suited for access by a
161scripting engine. The default is <tt>TRUE</tt>. Instead of <tt>TRUE</tt> or <tt>FALSE</tt>,
162you can specify a boolean member function.
163<p> <tt>STORED</tt> declares whether the property's value must be remembered
164when storing an object's state. Stored makes only sense for writable
165properties. The default value is <tt>TRUE</tt>. Technically superfluous
166properties (like <a href="qpoint.html">QPoint</a> pos if <a href="qrect.html">QRect</a> geometry is already a property)
167define this to be <tt>FALSE</tt>.
168<p> Connected to the property system is an additional macro, "Q_CLASSINFO",
169that can be used to attach additional name/value-pairs to a class'
170meta object, for example:
171<p> <pre>
172 Q_CLASSINFO( "Version", "3.0.0" )
173</pre>
174
175<p> Like other meta data, class information is accessible at runtime
176through the meta object, see <a href="qmetaobject.html#classInfo">QMetaObject::classInfo</a>() for details.
177<p> <a name="override"></a>
178<h2> Q_OVERRIDE
179</h2>
180<a name="1"></a><p> When you inherit a <a href="qobject.html">QObject</a> subclass you may wish to override some
181aspects of some of the class's properties.
182<p> For example, in <a href="qwidget.html">QWidget</a> we have the autoMask property defined like
183this:
184<pre>
185 Q_PROPERTY( bool autoMask READ autoMask WRITE setAutoMask DESIGNABLE false SCRIPTABLE false )
186</pre>
187
188<p> But we need to make the auto mask property designable in some QWidget
189subclasses. Similarly some classes will need this property to be
190scriptable (e.g. for QSA). This is achieved by overriding these
191features of the property in a subclass. In <a href="qcheckbox.html">QCheckBox</a>, for example, we
192achieve this using the following code:
193<pre>
194 Q_OVERRIDE( bool autoMask DESIGNABLE true SCRIPTABLE true )
195</pre>
196
197<p> Another example is <a href="qtoolbutton.html">QToolButton</a>. By default QToolButton has a read-only
198"toggleButton" property, because that's what it inherits from QButton:
199<pre>
200 Q_PROPERTY( bool toggleButton READ isToggleButton )
201</pre>
202
203<p> But we want to make our tool buttons able to be toggled, so we write a
204WRITE function in QToolButton, and use the following property override
205to make it acessible:
206<pre>
207 Q_OVERRIDE( bool toggleButton WRITE setToggleButton )
208</pre>
209
210The result is read-write (and scriptable and designable, since we now
211have a WRITE function) boolean property "toggleButton" for tool
212buttons.
213<p>
214<!-- eof -->
215<p><address><hr><div align=center>
216<table width=100% cellspacing=0 border=0><tr>
217<td>Copyright &copy; 2007
218<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
219<td align=right><div align=right>Qt 3.3.8</div>
220</table></div></address></body>
221</html>
Note: See TracBrowser for help on using the repository browser.