1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
<?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" />
<!-- extending-tutorial.qdoc -->
<title>Qt 4.8: Chapter 3: Adding Property Bindings</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>QML Examples & Demos</li>
<li>Chapter 3: Adding Property Bindings</li>
</ul>
</div>
</div>
<div class="content mainContent">
<h1 class="title">Chapter 3: Adding Property Bindings</h1>
<span class="subtitle"></span>
<!-- $$$declarative/tutorials/extending/chapter3-bindings-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="declarative-tutorials-extending-chapter3-bindings-app-qml.html">declarative/tutorials/extending/chapter3-bindings/app.qml</a></li>
<li><a href="declarative-tutorials-extending-chapter3-bindings-piechart-cpp.html">declarative/tutorials/extending/chapter3-bindings/piechart.cpp</a></li>
<li><a href="declarative-tutorials-extending-chapter3-bindings-piechart-h.html">declarative/tutorials/extending/chapter3-bindings/piechart.h</a></li>
<li><a href="declarative-tutorials-extending-chapter3-bindings-main-cpp.html">declarative/tutorials/extending/chapter3-bindings/main.cpp</a></li>
<li><a href="declarative-tutorials-extending-chapter3-bindings-chapter3-bindings-pro.html">declarative/tutorials/extending/chapter3-bindings/chapter3-bindings.pro</a></li>
</ul>
<p>Property bindings is a powerful feature of QML that allows values of different elements to be synchronized automatically. It uses signals to notify and update other elements' values when property values are changed.</p>
<p>Let's enable property bindings for the <tt>color</tt> property. That means if we have code like this:</p>
<pre class="qml"> import Charts 1.0
import QtQuick 1.0
<span class="type"><a href="qml-item.html">Item</a></span> {
<span class="name">width</span>: <span class="number">300</span>; <span class="name">height</span>: <span class="number">200</span>
<span class="type"><a href="qml-row.html">Row</a></span> {
<span class="name">anchors</span>.centerIn: <span class="name">parent</span>
<span class="name">spacing</span>: <span class="number">20</span>
<span class="type">PieChart</span> {
<span class="name">id</span>: <span class="name">chartA</span>
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
<span class="name">color</span>: <span class="string">"red"</span>
}
<span class="type">PieChart</span> {
<span class="name">id</span>: <span class="name">chartB</span>
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
<span class="name">color</span>: <span class="name">chartA</span>.<span class="name">color</span>
}
}
<span class="type"><a href="qml-mousearea.html">MouseArea</a></span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">onClicked</span>: { <span class="name">chartA</span>.<span class="name">color</span> <span class="operator">=</span> <span class="string">"blue"</span> }
}
<span class="type"><a href="qml-text.html">Text</a></span> {
<span class="type">anchors</span> { <span class="name">bottom</span>: <span class="name">parent</span>.<span class="name">bottom</span>; <span class="name">horizontalCenter</span>: <span class="name">parent</span>.<span class="name">horizontalCenter</span>; <span class="name">bottomMargin</span>: <span class="number">20</span> }
<span class="name">text</span>: <span class="string">"Click anywhere to change the chart color"</span>
}
}</pre>
<p class="centerAlign"><img src="images/extending-tutorial-chapter3.png" alt="" /></p><p>The "color: chartA.color" statement binds the <tt>color</tt> value of <tt>chartB</tt> to the <tt>color</tt> of <tt>chartA</tt>. Whenever <tt>chartA</tt>'s <tt>color</tt> value changes, <tt>chartB</tt>'s <tt>color</tt> value updates to the same value. When the window is clicked, the <tt>onClicked</tt> handler in the <a href="qml-mousearea.html">MouseArea</a> changes the color of <tt>chartA</tt>, thereby changing both charts to the color blue.</p>
<p>It's easy to enable property binding for the <tt>color</tt> property. We add a <a href="properties.html#qt-s-property-system">NOTIFY</a> feature to its <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>() declaration to indicate that a "colorChanged" signal is emitted whenever the value changes.</p>
<pre class="cpp"> <span class="keyword">class</span> PieChart : <span class="keyword">public</span> <span class="type"><a href="qdeclarativeitem.html">QDeclarativeItem</a></span>
{
...
Q_PROPERTY(<span class="type"><a href="qcolor.html">QColor</a></span> color READ color WRITE setColor NOTIFY colorChanged)
<span class="keyword">public</span>:
...
<span class="keyword">signals</span>:
<span class="type">void</span> colorChanged();
...
};</pre>
<p>Then, we emit this signal in <tt>setPieSlice()</tt>:</p>
<pre class="cpp"> <span class="type">void</span> PieChart<span class="operator">::</span>setColor(<span class="keyword">const</span> <span class="type"><a href="qcolor.html">QColor</a></span> <span class="operator">&</span>color)
{
<span class="keyword">if</span> (color <span class="operator">!</span><span class="operator">=</span> m_color) {
m_color <span class="operator">=</span> color;
update(); <span class="comment">// repaint with the new color</span>
<span class="keyword">emit</span> colorChanged();
}
}</pre>
<p>It's important for <tt>setColor()</tt> to check that the color value has actually changed before emitting <tt>colorChanged()</tt>. This ensures the signal is not emitted unnecessarily and also prevents loops when other elements respond to the value change.</p>
<p>The use of bindings is essential to QML. You should always add NOTIFY signals for properties if they are able to be implemented, so that your properties can be used in bindings. Properties that cannot be bound cannot be automatically updated and cannot be used as flexibly in QML. Also, since bindings are invoked so often and relied upon in QML usage, users of your custom QML types may see unexpected behavior if bindings are not implemented.</p>
</div>
<!-- @@@declarative/tutorials/extending/chapter3-bindings -->
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation 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>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
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>
</body>
</html>
|