Sophie

Sophie

distrib > Mageia > 5 > i586 > media > core-release > by-pkgid > 50facae208d4a6f280e44a513b104320 > files > 33

qt-mobility-doc-1.2.0-13.mga5.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" />
<!-- battery-charge.qdoc -->
  <title>Qt Mobility 1.2: Accessing Publish and Subscribe from QML</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>QtMobility 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="http://qt.nokia.com/doc/4.7/all-examples.html">Examples</a></li>
<li>Accessing Publish and Subscribe from QML</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level2"><a href="#battery-subscriber">Battery Subscriber</a></li>
<li class="level3"><a href="#qml">QML</a></li>
</ul>
</div>
<h1 class="title">Accessing Publish and Subscribe from QML</h1>
<span class="subtitle"></span>
<!-- $$$battery-charge-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="battery-charge-battery-publisher-batterypublisher-cpp.html">battery-charge/battery-publisher/batterypublisher.cpp</a></li>
<li><a href="battery-charge-battery-publisher-batterypublisher-h.html">battery-charge/battery-publisher/batterypublisher.h</a></li>
<li><a href="battery-charge-battery-publisher-batterypublisher-ui.html">battery-charge/battery-publisher/batterypublisher.ui</a></li>
<li><a href="battery-charge-battery-subscriber-battery-subscriber-qml.html">battery-charge/battery-subscriber/battery-subscriber.qml</a></li>
<li><a href="battery-charge-battery-publisher-main-cpp.html">battery-charge/battery-publisher/main.cpp</a></li>
<li><a href="battery-charge-battery-charge-pro.html">battery-charge/battery-charge.pro</a></li>
<li><a href="battery-charge-battery-publisher-battery-publisher-pro.html">battery-charge/battery-publisher/battery-publisher.pro</a></li>
</ul>
<p>This example shows how to access published context values from within QML. The example consists of two programs. The first, battery-publisher, is a standard Qt GUI application that is used to emulate a battery for the sole purpose of demonstrating the second program. It publishes the keys</p>
<pre class="cpp"> <span class="operator">/</span>power<span class="operator">/</span>battery<span class="operator">/</span>charge
 <span class="operator">/</span>power<span class="operator">/</span>battery<span class="operator">/</span>charging</pre>
<p>and provides controls for modifying their values.</p>
<p class="centerAlign"><img src="images/battery-publisher.png" alt="" /></p><p>The second program, namely battery-subscriber, consists of a QML application that accesses the actual value of the published battery-charge property via the <a href="qml-valuespacesubscriber.html">ValueSpaceSubscriber</a> QML element and displays the value to the end-user.</p>
<p>The user interface of the battery subscriber program is described in QML. It has the following features: A rectangular area representing the percent charge of the battery. It indicates a low battery state by changing the color to red, it is green otherwise. An animation is shown to indicate that the battery is being recharged.</p>
<p class="centerAlign"><img src="images/battery-subscriber.png" alt="" /></p><a name="battery-subscriber"></a>
<h3>Battery Subscriber</h3>
<p>The <a href="qvaluespacesubscriber.html">QValueSpaceSubscriber</a> class is available from within QML through the use of a plugin which is parsed by the QML engine. This means that simply using the QML element '<a href="qml-valuespacesubscriber.html">ValueSpaceSubscriber</a>' allows access to the valuespace, which is demonstrated below.</p>
<a name="qml"></a>
<h4>QML</h4>
<p>Firstly, import the plugin library that provides the <a href="qml-valuespacesubscriber.html">ValueSpaceSubscriber</a> QML element.</p>
<pre class="qml"> import QtMobility.publishsubscribe 1.1</pre>
<p>Two <a href="qml-valuespacesubscriber.html">ValueSpaceSubscriber</a> instances are created, one for each of the battery values. We give each object a unique id so that we can reference it from elsewhere in the QML. We set the <i>path</i> properties to the Value Space path of the keys. Finally we set the <i>notify</i> properties to true to enable the emission of change notification signals.</p>
<pre class="qml"> <span class="type"><a href="qml-valuespacesubscriber.html">ValueSpaceSubscriber</a></span> {
     <span class="name">id</span>: <span class="name">batteryCharge</span>
     <span class="name">path</span>: <span class="string">&quot;/power/battery/charge&quot;</span>
 }
 <span class="type"><a href="qml-valuespacesubscriber.html">ValueSpaceSubscriber</a></span> {
     <span class="name">id</span>: <span class="name">batteryCharging</span>
     <span class="name">path</span>: <span class="string">&quot;/power/battery/charging&quot;</span>
 }</pre>
<p>The default state of the rectangle used to visualize the battery charge uses the <i>charge</i> property of our BatteryCharge class in the expression for its height.</p>
<pre class="qml"> <span class="name">id</span>: <span class="name">visualCharge</span>
 <span class="name">x</span>: <span class="number">12</span>
 <span class="name">y</span>: <span class="number">22</span> <span class="operator">+</span> <span class="number">196</span> <span class="operator">-</span> <span class="name">height</span>
 <span class="name">width</span>: <span class="number">76</span>
 <span class="name">height</span>: <span class="number">196</span> <span class="operator">*</span> <span class="name">batteryCharge</span>.<span class="name">value</span> <span class="operator">/</span> <span class="number">100</span>
 <span class="name">clip</span>: <span class="number">true</span>
 <span class="name">color</span>: <span class="string">&quot;green&quot;</span></pre>
<p>When the battery charge changes the height of the rectangle will automatically change.</p>
<p>Next we define two additional states. The <i>low</i> state is entered when the battery charge drops below 25% and the battery is not being recharged. When in this state the color is set to red.</p>
<pre class="qml"> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qml-state.html">State</a></span> {
     <span class="name">name</span>: <span class="string">&quot;low&quot;</span>
     <span class="name">when</span>: <span class="name">batteryCharge</span>.<span class="name">value</span> <span class="operator">&lt;</span> <span class="number">25</span> <span class="operator">&amp;&amp;</span> !<span class="name">batteryCharging</span>.<span class="name">value</span>
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qml-propertychanges.html">PropertyChanges</a></span> {
         <span class="name">target</span>: <span class="name">visualCharge</span>
         <span class="name">color</span>: <span class="string">&quot;red&quot;</span>
     }
 }</pre>
<p>The <i>charging</i> state is entered when the battery is being recharged. When in this state a particle effect animation is enabled.</p>
<pre class="qml"> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qml-state.html">State</a></span> {
     <span class="name">name</span>: <span class="string">&quot;charging&quot;</span>
     <span class="name">when</span>: <span class="name">batteryCharging</span>.<span class="name">value</span>
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qml-propertychanges.html">PropertyChanges</a></span> {
         <span class="name">target</span>: <span class="name">bubbles</span>
         <span class="name">count</span>: <span class="name">batteryCharge</span>.<span class="name">value</span> <span class="operator">/</span> <span class="number">5</span>
         <span class="name">emissionRate</span>: <span class="number">5</span>
     }
 },</pre>
</div>
<!-- @@@battery-charge -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
  <p>
     <acronym title="Copyright">&copy;</acronym> 2008-2011 Nokia Corporation and/or its
     subsidiaries. Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
     in Finland and/or other countries worldwide.</p>
  <p>
     All other trademarks are property of their respective owners. <a title="Privacy Policy"
     href="http://qt.nokia.com/about/privacy-policy">Privacy Policy</a></p>
  <br />
  <p>
    Licensees holding valid Qt Commercial licenses may use this document in accordance with the    Qt Commercial License Agreement provided with the Software or, alternatively, in accordance    with the terms contained in a written agreement between you and Nokia.</p>
  <p>
    Alternatively, this document may be used 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>
</div>
</body>
</html>