Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 4430

qt-doc-4.8.5-10.fc18.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- properties.qdoc -->
  <title>Qt 4.8: The Property System</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li>The Property System</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#requirements-for-declaring-properties">Requirements for Declaring Properties</a></li>
<li class="level1"><a href="#reading-and-writing-properties-with-the-meta-object-system">Reading and Writing Properties with the Meta-Object System</a></li>
<li class="level1"><a href="#a-simple-example">A Simple Example</a></li>
<li class="level1"><a href="#dynamic-properties">Dynamic Properties</a></li>
<li class="level1"><a href="#properties-and-custom-types">Properties and Custom Types</a></li>
<li class="level1"><a href="#adding-additional-information-to-a-class">Adding Additional Information to a Class</a></li>
</ul>
</div>
<h1 class="title">The Property System</h1>
<span class="subtitle"></span>
<!-- $$$properties.html-description -->
<div class="descr"> <a name="details"></a>
<a name="qt-s-property-system"></a><p>Qt provides a sophisticated property system similar to the ones supplied by some compiler vendors. However, as a compiler- and platform-independent library, Qt does not rely on non-standard compiler features like <tt>__property</tt> or <tt>[property]</tt>. The Qt solution works with <i>any</i> standard C++ compiler on every platform Qt supports. It is based on the <a href="metaobjects.html#meta-object-system">Meta-Object System</a> that also provides inter-object communication via <a href="signalsandslots.html#signals-and-slots">signals and slots</a>.</p>
<a name="requirements-for-declaring-properties"></a>
<h2>Requirements for Declaring Properties</h2>
<p>To declare a property, use the <a href="qobject.html#Q_PROPERTY">Q_PROPERTY()</a> macro in a class that inherits <a href="qobject.html">QObject</a>.</p>
<pre class="cpp"> <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>(type name
            READ getFunction
            <span class="operator">[</span>WRITE setFunction<span class="operator">]</span>
            <span class="operator">[</span>RESET resetFunction<span class="operator">]</span>
            <span class="operator">[</span>NOTIFY notifySignal<span class="operator">]</span>
            <span class="operator">[</span>REVISION <span class="type">int</span><span class="operator">]</span>
            <span class="operator">[</span>DESIGNABLE <span class="type">bool</span><span class="operator">]</span>
            <span class="operator">[</span>SCRIPTABLE <span class="type">bool</span><span class="operator">]</span>
            <span class="operator">[</span>STORED <span class="type">bool</span><span class="operator">]</span>
            <span class="operator">[</span>USER <span class="type">bool</span><span class="operator">]</span>
            <span class="operator">[</span>CONSTANT<span class="operator">]</span>
            <span class="operator">[</span>FINAL<span class="operator">]</span>)</pre>
<p>Here are some typical examples of property declarations taken from class <a href="qwidget.html">QWidget</a>.</p>
<pre class="cpp"> <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>(<span class="type">bool</span> focus READ hasFocus)
 <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>(<span class="type">bool</span> enabled READ isEnabled WRITE setEnabled)
 <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>(<span class="type"><a href="qcursor.html">QCursor</a></span> cursor READ cursor WRITE setCursor RESET unsetCursor)</pre>
<p>A property behaves like a class data member, but it has additional features accessible through the <a href="metaobjects.html#meta-object-system">Meta-Object System</a>.</p>
<ul>
<li>A <tt>READ</tt> accessor function is required. It is for reading the property value. Ideally, a const function is used for this purpose, and it must return either the property's type or a pointer or reference to that type. e.g&#x2e;, <a href="qwidget.html#focus-prop">QWidget::focus</a> is a read-only property with <tt>READ</tt> function, <a href="qwidget.html#focus-prop">QWidget::hasFocus</a>().</li>
<li>A <tt>WRITE</tt> accessor function is optional. It is for setting the property value. It must return void and must take exactly one argument, either of the property's type or a pointer or reference to that type. e.g&#x2e;, <a href="qwidget.html#enabled-prop">QWidget::enabled</a> has the <tt>WRITE</tt> function <a href="qwidget.html#enabled-prop">QWidget::setEnabled</a>(). Read-only properties do not need <tt>WRITE</tt> functions. e.g&#x2e;, <a href="qwidget.html#focus-prop">QWidget::focus</a> has no <tt>WRITE</tt> function.</li>
<li>A <tt>RESET</tt> function is optional. It is for setting the property back to its context specific default value. e.g&#x2e;, <a href="qwidget.html#cursor-prop">QWidget::cursor</a> has the typical <tt>READ</tt> and <tt>WRITE</tt> functions, <a href="qwidget.html#cursor-prop">QWidget::cursor</a>() and <a href="qwidget.html#cursor-prop">QWidget::setCursor</a>(), and it also has a <tt>RESET</tt> function, <a href="qwidget.html#cursor-prop">QWidget::unsetCursor</a>(), since no call to <a href="qwidget.html#cursor-prop">QWidget::setCursor</a>() can mean <i>reset to the context specific cursor</i>. The <tt>RESET</tt> function must return void and take no parameters.</li>
<li>A <tt>NOTIFY</tt> signal is optional. If defined, it should specify one existing signal in that class that is emitted whenever the value of the property changes.</li>
<li>A <tt>REVISION</tt> number is optional. If included, it defines the the property and its notifier signal to be used in a particular revision of the API that is exposed to QML.</li>
<li>The <tt>DESIGNABLE</tt> attribute indicates whether the property should be visible in the property editor of GUI design tool (e.g&#x2e;, <a href="designer-manual.html#qt-designer">Qt Designer</a>). Most properties are <tt>DESIGNABLE</tt> (default true). Instead of true or false, you can specify a boolean member function.</li>
<li>The <tt>SCRIPTABLE</tt> attribute indicates whether this property should be accessible by a scripting engine (default true). Instead of true or false, you can specify a boolean member function.</li>
<li>The <tt>STORED</tt> attribute indicates whether the property should be thought of as existing on its own or as depending on other values. It also indicates whether the property value must be saved when storing the object's state. Most properties are <tt>STORED</tt> (default true), but e.g&#x2e;, <a href="qwidget.html#minimumWidth-prop">QWidget::minimumWidth</a>() has <tt>STORED</tt> false, because its value is just taken from the width component of property <a href="qwidget.html#minimumSize-prop">QWidget::minimumSize</a>(), which is a <a href="qsize.html">QSize</a>.</li>
<li>The <tt>USER</tt> attribute indicates whether the property is designated as the user-facing or user-editable property for the class. Normally, there is only one <tt>USER</tt> property per class (default false). e.g&#x2e;, <a href="qabstractbutton.html#checked-prop">QAbstractButton::checked</a> is the user editable property for (checkable) buttons. Note that <a href="qitemdelegate.html">QItemDelegate</a> gets and sets a widget's <tt>USER</tt> property.</li>
<li>The presence of the <tt>CONSTANT</tt> attibute indicates that the property value is constant. For a given object instance, the READ method of a constant property must return the same value every time it is called. This constant value may be different for different instances of the object. A constant property cannot have a WRITE method or a NOTIFY signal.</li>
<li>The presence of the <tt>FINAL</tt> attribute indicates that the property will not be overridden by a derived class. This can be used for performance optimizations in some cases, but is not enforced by moc. Care must be taken never to override a <tt>FINAL</tt> property.</li>
</ul>
<p>The <tt>READ</tt>, <tt>WRITE</tt>, and <tt>RESET</tt> functions can be inherited. They can also be virtual. When they are inherited in classes where multiple inheritance is used, they must come from the first inherited class.</p>
<p>The property type can be any type supported by <a href="qvariant.html">QVariant</a>, or it can be a user-defined type. In this example, class <a href="qdate.html">QDate</a> is considered to be a user-defined type.</p>
<pre class="cpp"> <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>(<span class="type"><a href="qdate.html">QDate</a></span> date READ getDate WRITE setDate)</pre>
<p>Because <a href="qdate.html">QDate</a> is user-defined, you must include the <tt>&lt;QDate&gt;</tt> header file with the property declaration.</p>
<p>For <a href="qmap.html">QMap</a>, <a href="qlist.html">QList</a>, and QValueList properties, the property value is a <a href="qvariant.html">QVariant</a> whose value is the entire list or map. Note that the Q_PROPERTY string cannot contain commas, because commas separate macro arguments. Therefore, you must use <tt>QMap</tt> as the property type instead of <tt>QMap&lt;QString,QVariant&gt;</tt>. For consistency, also use <tt>QList</tt> and <tt>QValueList</tt> instead of <tt>QList&lt;QVariant&gt;</tt> and <tt>QValueList&lt;QVariant&gt;</tt>.</p>
<a name="reading-and-writing-properties-with-the-meta-object-system"></a>
<h2>Reading and Writing Properties with the Meta-Object System</h2>
<p>A property can be read and written using the generic functions <a href="qobject.html#property">QObject::property</a>() and <a href="qobject.html#setProperty">QObject::setProperty</a>(), without knowing anything about the owning class except the property's name. In the code snippet below, the call to <a href="qabstractbutton.html#down-prop">QAbstractButton::setDown</a>() and the call to <a href="qobject.html#setProperty">QObject::setProperty</a>() both set property &quot;down&quot;.</p>
<pre class="cpp"> <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>button <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>;
 <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>object <span class="operator">=</span> button;

 button<span class="operator">-</span><span class="operator">&gt;</span>setDown(<span class="keyword">true</span>);
 object<span class="operator">-</span><span class="operator">&gt;</span>setProperty(<span class="string">&quot;down&quot;</span><span class="operator">,</span> <span class="keyword">true</span>);</pre>
<p>Accessing a property through its <tt>WRITE</tt> accessor is the better of the two, because it is faster and gives better diagnostics at compile time, but setting the property this way requires that you know about the class at compile time. Accessing properties by name lets you access classes you don't know about at compile time. You can <i>discover</i> a class's properties at run time by querying its <a href="qobject.html">QObject</a>, <a href="qmetaobject.html">QMetaObject</a>, and <a href="qmetaproperty.html">QMetaProperties</a>.</p>
<pre class="cpp"> <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>object <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
 <span class="keyword">const</span> <span class="type"><a href="qmetaobject.html">QMetaObject</a></span> <span class="operator">*</span>metaobject <span class="operator">=</span> object<span class="operator">-</span><span class="operator">&gt;</span>metaObject();
 <span class="type">int</span> count <span class="operator">=</span> metaobject<span class="operator">-</span><span class="operator">&gt;</span>propertyCount();
 <span class="keyword">for</span> (<span class="type">int</span> i<span class="operator">=</span><span class="number">0</span>; i<span class="operator">&lt;</span>count; <span class="operator">+</span><span class="operator">+</span>i) {
     <span class="type"><a href="qmetaproperty.html">QMetaProperty</a></span> metaproperty <span class="operator">=</span> metaobject<span class="operator">-</span><span class="operator">&gt;</span>property(i);
     <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>name <span class="operator">=</span> metaproperty<span class="operator">.</span>name();
     <span class="type"><a href="qvariant.html">QVariant</a></span> value <span class="operator">=</span> object<span class="operator">-</span><span class="operator">&gt;</span>property(name);
     <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
 }</pre>
<p>In the above snippet, <a href="qmetaobject.html#property">QMetaObject::property</a>() is used to get <a href="qmetaproperty.html">metadata</a> about each property defined in some unknown class. The property name is fetched from the metadata and passed to <a href="qobject.html#property">QObject::property</a>() to get the <a href="qvariant.html">value</a> of the property in the current <a href="qobject.html">object</a>.</p>
<a name="a-simple-example"></a>
<h2>A Simple Example</h2>
<p>Suppose we have a class MyClass, which is derived from <a href="qobject.html">QObject</a> and which uses the <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> macro in its private section. We want to declare a property in MyClass to keep track of a priorty value. The name of the property will be <i>priority</i>, and its type will be an enumeration type named <i>Priority</i>, which is defined in MyClass.</p>
<p>We declare the property with the <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>() macro in the private section of the class. The required <tt>READ</tt> function is named <tt>priority</tt>, and we include a <tt>WRITE</tt> function named <tt>setPriority</tt>. The enumeration type must be registered with the <a href="metaobjects.html#meta-object-system">Meta-Object System</a> using the <a href="qobject.html#Q_ENUMS">Q_ENUMS</a>() macro. Registering an enumeration type makes the enumerator names available for use in calls to <a href="qobject.html#setProperty">QObject::setProperty</a>(). We must also provide our own declarations for the <tt>READ</tt> and <tt>WRITE</tt> functions. The declaration of MyClass then might look like this:</p>
<pre class="cpp"> <span class="keyword">class</span> MyClass : <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span>
 {
     Q_OBJECT
     Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
     Q_ENUMS(Priority)

 <span class="keyword">public</span>:
     MyClass(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
     <span class="operator">~</span>MyClass();

     <span class="keyword">enum</span> Priority { High<span class="operator">,</span> Low<span class="operator">,</span> VeryHigh<span class="operator">,</span> VeryLow };

     <span class="type">void</span> setPriority(Priority priority)
     {
         m_priority <span class="operator">=</span> priority;
         <span class="keyword">emit</span> priorityChanged(priority);
     }
     Priority priority() <span class="keyword">const</span>
     { <span class="keyword">return</span> m_priority; }

 <span class="keyword">signals</span>:
     <span class="type">void</span> priorityChanged(Priority);

 <span class="keyword">private</span>:
     Priority m_priority;
 };</pre>
<p>The <tt>READ</tt> function is const and returns the property type. The <tt>WRITE</tt> function returns void and has exactly one parameter of the property type. The meta-object compiler enforces these requirements.</p>
<p>Given a pointer to an instance of MyClass or a pointer to a <a href="qobject.html">QObject</a> that is an instance of MyClass, we have two ways to set its priority property:</p>
<pre class="cpp"> MyClass <span class="operator">*</span>myinstance <span class="operator">=</span> <span class="keyword">new</span> MyClass;
 <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>object <span class="operator">=</span> myinstance;

 myinstance<span class="operator">-</span><span class="operator">&gt;</span>setPriority(MyClass<span class="operator">::</span>VeryHigh);
 object<span class="operator">-</span><span class="operator">&gt;</span>setProperty(<span class="string">&quot;priority&quot;</span><span class="operator">,</span> <span class="string">&quot;VeryHigh&quot;</span>);</pre>
<p>In the example, the enumeration type that is the property type is declared in MyClass and registered with the <a href="metaobjects.html#meta-object-system">Meta-Object System</a> using the <a href="qobject.html#Q_ENUMS">Q_ENUMS</a>() macro. This makes the enumeration values available as strings for use as in the call to setProperty(). Had the enumeration type been declared in another class, its fully qualified name (i.e&#x2e;, OtherClass::Priority) would be required, and that other class would also have to inherit <a href="qobject.html">QObject</a> and register the enumeration type there using the <a href="qobject.html#Q_ENUMS">Q_ENUMS</a>() macro.</p>
<p>A similar macro, <a href="qobject.html#Q_FLAGS">Q_FLAGS</a>(), is also available. Like <a href="qobject.html#Q_ENUMS">Q_ENUMS</a>(), it registers an enumeration type, but it marks the type as being a set of <i>flags</i>, i.e&#x2e; values that can be OR'd together. An I/O class might have enumeration values <tt>Read</tt> and <tt>Write</tt> and then <a href="qobject.html#setProperty">QObject::setProperty</a>() could accept <tt>Read | Write</tt>. <a href="qobject.html#Q_FLAGS">Q_FLAGS</a>() should be used to register this enumeration type.</p>
<a name="dynamic-properties"></a>
<h2>Dynamic Properties</h2>
<p><a href="qobject.html#setProperty">QObject::setProperty</a>() can also be used to add <i>new</i> properties to an instance of a class at runtime. When it is called with a name and a value, if a property with the given name exists in the <a href="qobject.html">QObject</a>, and if the given value is compatible with the property's type, the value is stored in the property, and true is returned. If the value is <i>not</i> compatible with the property's type, the property is <i>not</i> changed, and false is returned. But if the property with the given name doesn't exist in the <a href="qobject.html">QObject</a> (i.e&#x2e;, if it wasn't declared with <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>(), a new property with the given name and value is automatically added to the <a href="qobject.html">QObject</a>, but false is still returned. This means that a return of false can't be used to determine whether a particular property was actually set, unless you know in advance that the property already exists in the <a href="qobject.html">QObject</a>.</p>
<p>Note that <i>dynamic</i> properties are added on a per instance basis, i.e&#x2e;, they are added to <a href="qobject.html">QObject</a>, not <a href="qmetaobject.html">QMetaObject</a>. A property can be removed from an instance by passing the property name and an invalid <a href="qvariant.html">QVariant</a> value to <a href="qobject.html#setProperty">QObject::setProperty</a>(). The default constructor for <a href="qvariant.html">QVariant</a> constructs an invalid <a href="qvariant.html">QVariant</a>.</p>
<p>Dynamic properties can be queried with <a href="qobject.html#property">QObject::property</a>(), just like properties declared at compile time with <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>().</p>
<a name="properties-and-custom-types"></a>
<h2>Properties and Custom Types</h2>
<p>Custom types used by properties need to be registered using the <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>() macro so that their values can be stored in <a href="qvariant.html">QVariant</a> objects. This makes them suitable for use with both static properties declared using the <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>() macro in class definitions and dynamic properties created at run-time.</p>
<a name="adding-additional-information-to-a-class"></a>
<h2>Adding Additional Information to a Class</h2>
<p>Connected to the property system is an additional macro, <a href="qobject.html#Q_CLASSINFO">Q_CLASSINFO</a>(), that can be used to attach additional <i>name</i>--<i>value</i> pairs to a class's meta-object, for example:</p>
<pre class="cpp"> <a href="qobject.html#Q_CLASSINFO">Q_CLASSINFO</a>(<span class="string">&quot;Version&quot;</span><span class="operator">,</span> <span class="string">&quot;3.0.0&quot;</span>)</pre>
<p>Like other meta-data, class information is accessible at run-time through the meta-object; see <a href="qmetaobject.html#classInfo">QMetaObject::classInfo</a>() for details.</p>
</div>
<p><b>See also </b><a href="metaobjects.html#meta-object-system">Meta-Object System</a>, <a href="signalsandslots.html#signals-and-slots">Signals and Slots</a>, <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>(), <a href="qmetatype.html">QMetaType</a>, and <a href="qvariant.html">QVariant</a>.</p>
<!-- @@@properties.html -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  </div>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>