Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 1dd17e0d683ef79b4bb6872bbf359d7f > files > 1226

qt4-doc-4.8.7-26.2.mga7.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" />
<!-- qml-qtbubblelevel.qdoc -->
  <title>Qt 4.8: Qt Bubble Level Demo</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><a href="all-examples.html">Examples</a></li>
<li>Qt Bubble Level Demo</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="#initialising-the-application">Initialising the application</a></li>
<li class="level1"><a href="#accessing-the-accelerometer-information">Accessing the accelerometer information</a></li>
<li class="level1"><a href="#the-qt-quick-ui">The Qt Quick UI</a></li>
</ul>
</div>
<h1 class="title">Qt Bubble Level Demo</h1>
<span class="subtitle"></span>
<!-- $$$demos/mobile/qtbubblelevel-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="demos-mobile-qtbubblelevel-accelerometerfilter-cpp.html">demos/mobile/qtbubblelevel/accelerometerfilter.cpp</a></li>
<li><a href="demos-mobile-qtbubblelevel-accelerometerfilter-h.html">demos/mobile/qtbubblelevel/accelerometerfilter.h</a></li>
<li><a href="demos-mobile-qtbubblelevel-settings-h.html">demos/mobile/qtbubblelevel/settings.h</a></li>
<li><a href="demos-mobile-qtbubblelevel-taskswitcher-cpp.html">demos/mobile/qtbubblelevel/taskswitcher.cpp</a></li>
<li><a href="demos-mobile-qtbubblelevel-taskswitcher-h.html">demos/mobile/qtbubblelevel/taskswitcher.h</a></li>
<li><a href="demos-mobile-qtbubblelevel-icons-bubblelevel-svg.html">demos/mobile/qtbubblelevel/icons/bubblelevel.svg</a></li>
<li><a href="demos-mobile-qtbubblelevel-qml-bubblelevel-qml.html">demos/mobile/qtbubblelevel/qml/BubbleLevel.qml</a></li>
<li><a href="demos-mobile-qtbubblelevel-qml-button-qml.html">demos/mobile/qtbubblelevel/qml/Button.qml</a></li>
<li><a href="demos-mobile-qtbubblelevel-qml-tube-qml.html">demos/mobile/qtbubblelevel/qml/Tube.qml</a></li>
<li><a href="demos-mobile-qtbubblelevel-main-cpp.html">demos/mobile/qtbubblelevel/main.cpp</a></li>
<li><a href="demos-mobile-qtbubblelevel-qtbubblelevel-pro.html">demos/mobile/qtbubblelevel/qtbubblelevel.pro</a></li>
<li><a href="demos-mobile-qtbubblelevel-resources-qrc.html">demos/mobile/qtbubblelevel/resources.qrc</a></li>
</ul>
<p>Qt Bubble Level is a simple application that uses Qt Mobility's accelerometer APIs and hardware sensor information to calculate the inclination of the device and presents this as atraditional bubble level.<p>The application provides a calibration feature to handle any possible errors in accelerometer readings. The example is hosted in Projects Forum Nokia: https://projects.forum.nokia.com/qtbubblelevel</p>
<p><b>Note:</b> This demonstration requires QtMobility libraries.</p>
<p class="centerAlign"><img src="images/qml-qtbubblelevel-demo.png" alt="" /></p><a name="initialising-the-application"></a>
<h2>Initialising the application</h2>
<p>All of the initialisations are done in the main function.</p>
<p>First, <a href="qdeclarativeview.html">QDeclarativeView</a> is created to intepret the QML files. The QML file given is found in the Qt resource. The root QML element is set to resize to the view whenever the view is resized.</p>
<pre class="cpp"> <span class="type"><a href="qdeclarativeview.html">QDeclarativeView</a></span> view;
 view<span class="operator">.</span>setSource(<span class="type"><a href="qurl.html">QUrl</a></span>(<span class="string">&quot;qrc:/qml/BubbleLevel.qml&quot;</span>));
 view<span class="operator">.</span>setResizeMode(<span class="type"><a href="qdeclarativeview.html">QDeclarativeView</a></span><span class="operator">::</span>SizeRootObjectToView);</pre>
<p>The Settings object will handle the loading or storing of the calibration value. Next, we create instances from QAccelerometer and AccelerometerFilter and attach the filter to the sensor.</p>
<pre class="cpp"> Settings settings;

 <span class="type">QAccelerometer</span> sensor;
 AccelerometerFilter filter;
 sensor<span class="operator">.</span>addFilter(<span class="operator">&amp;</span>filter);</pre>
<p>The Qt code is then connected to QML code by using Qt Signals and Slots connections. First, the root object is retrieved from <a href="qdeclarativeview.html">QDeclarativeView</a>. The root object now represents the Qt object of the QML root element.</p>
<p>The saveCorrectionAngle signal of the QML root element is connected to the Qt slot saveCorrectionAngle. The rotationChanged and correctionAngle Qt signals are connected to the handleRotation and setCorrectionAngle slot of the QML root element. Finally, the quit signal of <a href="qdeclarativeengine.html">QDeclarativeEngine</a> is connected to <a href="qapplication.html">QApplication</a>'s quit slot.</p>
<pre class="cpp"> <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>rootObject <span class="operator">=</span> <span class="keyword">dynamic_cast</span><span class="operator">&lt;</span><span class="type"><a href="qobject.html">QObject</a></span><span class="operator">*</span><span class="operator">&gt;</span>(view<span class="operator">.</span>rootObject());

 <span class="comment">// Associate Qt / QML signals and slots</span>
 <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(rootObject<span class="operator">,</span> SIGNAL(saveCorrectionAngle(<span class="keyword">const</span> <span class="type"><a href="qvariant.html">QVariant</a></span><span class="operator">&amp;</span>))<span class="operator">,</span>
                  <span class="operator">&amp;</span>settings<span class="operator">,</span> SLOT(saveCorrectionAngle(<span class="keyword">const</span> <span class="type"><a href="qvariant.html">QVariant</a></span><span class="operator">&amp;</span>)));

 <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(<span class="operator">&amp;</span>filter<span class="operator">,</span> SIGNAL(rotationChanged(<span class="keyword">const</span> <span class="type"><a href="qvariant.html">QVariant</a></span><span class="operator">&amp;</span>))<span class="operator">,</span>
                  rootObject<span class="operator">,</span> SLOT(handleRotation(<span class="keyword">const</span> <span class="type"><a href="qvariant.html">QVariant</a></span><span class="operator">&amp;</span>)));

 <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(<span class="operator">&amp;</span>settings<span class="operator">,</span> SIGNAL(correctionAngle(<span class="keyword">const</span> <span class="type"><a href="qvariant.html">QVariant</a></span><span class="operator">&amp;</span>))<span class="operator">,</span>
                  rootObject<span class="operator">,</span> SLOT(setCorrectionAngle(<span class="keyword">const</span> <span class="type"><a href="qvariant.html">QVariant</a></span><span class="operator">&amp;</span>)));

 <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect((<span class="type"><a href="qobject.html">QObject</a></span><span class="operator">*</span>)view<span class="operator">.</span>engine()<span class="operator">,</span> SIGNAL(quit())<span class="operator">,</span>
                  <span class="operator">&amp;</span>app<span class="operator">,</span> SLOT(quit()));</pre>
<p>On the Maemo target, the application needs a minimise button, so we connect one additional QML signal to the Qt slot. The minimise button is made visible by setting the value of the QML root element's taskSwitcherVisible property to true.</p>
<pre class="cpp"> <span class="preprocessor">#ifdef Q_WS_MAEMO_5</span>
 TaskSwitcher taskSwitcher;

 <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(rootObject<span class="operator">,</span> SIGNAL(minimizeApplication())<span class="operator">,</span>
                  <span class="operator">&amp;</span>taskSwitcher<span class="operator">,</span> SLOT(minimizeApplication()));

 <span class="comment">// Show the task switcher button</span>
 rootObject<span class="operator">-</span><span class="operator">&gt;</span>setProperty(<span class="string">&quot;taskSwitcherVisible&quot;</span><span class="operator">,</span> <span class="keyword">true</span>);
 <span class="preprocessor">#endif</span></pre>
<p>The correction factor of the accelerometer is retrieved from persistent storage by using <a href="qsettings.html">QSettings</a>. The correction factor is signalled to the QML side by using the function setCorrectionAngle. The accelerometer sensor is started and it will eventually begin to signal the changes in accelerometer readings.</p>
<pre class="cpp"> <span class="comment">// Read correction factor from permanent storage and emit it to QML side</span>
 settings<span class="operator">.</span>loadAndEmitCorrectionAngle();

 <span class="comment">// Begin measuring of the accelerometer sensor</span>
 sensor<span class="operator">.</span>start();</pre>
<p>Finally, in the end of the function the view is shown in full screen on mobile devices. On other targets, the application is shown as 800 x 480 resolution in the 100, 100 position from the top-left corner of the desktop.</p>
<pre class="cpp"> <span class="preprocessor">#if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)</span>
 view<span class="operator">.</span>setGeometry(<span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>desktop()<span class="operator">-</span><span class="operator">&gt;</span>screenGeometry());
 view<span class="operator">.</span>showFullScreen();
 <span class="preprocessor">#else</span>
 view<span class="operator">.</span>setGeometry((<span class="type"><a href="qrect.html">QRect</a></span>(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span><span class="operator">,</span> <span class="number">800</span><span class="operator">,</span> <span class="number">480</span>)));
 view<span class="operator">.</span>show();
 <span class="preprocessor">#endif</span></pre>
<a name="accessing-the-accelerometer-information"></a>
<h2>Accessing the accelerometer information</h2>
<p>The inclination of the device is resolved by using the QAccelerometer sensor of QtMobility. We already created the sensor in the main function and attached our self-derived AccelerometerFilter object to it. Here is the definition of the AccelerometerFilter class:</p>
<pre class="cpp"> <span class="preprocessor">#include &lt;QAccelerometerFilter&gt;</span>
 <span class="preprocessor">#include &lt;QVariant&gt;</span>

 QTM_USE_NAMESPACE

 <span class="keyword">class</span> AccelerometerFilter
     : <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">,</span> <span class="keyword">public</span> <span class="type">QAccelerometerFilter</span>
 {
     Q_OBJECT

 <span class="keyword">protected</span>:
     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> x;
     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> y;
     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> z;

 <span class="keyword">public</span>:
     AccelerometerFilter();
     <span class="type">bool</span> filter(<span class="type">QAccelerometerReading</span> <span class="operator">*</span>reading);

 <span class="keyword">signals</span>:
     <span class="type">void</span> rotationChanged(<span class="keyword">const</span> <span class="type"><a href="qvariant.html">QVariant</a></span> <span class="operator">&amp;</span>deg);
 };</pre>
<p>The class is multiderived from <a href="qobject.html">QObject</a> and QAccelerometerFilter classes. The QAccelerometerFilter class is derived from <a href="qobject.html">QObject</a> because we want to use Qt Signals and Slots to signal changes in accelerometer readings.</p>
<p>The members x, y, and z store the previous values of the sensor reading in order to implement a low pass filter to the values.</p>
<p>In the implementation of the AccelerometerFilter class, we first read the value of each axis from the QAccelerometerReading object. The values are then converted from radians to degrees and applied the low pass filter to reduce noise in the sensor readings. Different low pass factors are used depending on the platform (these were determined to be good via experimenting). Finally, the calculated value is emitted.</p>
<p>Note that the accelerometer sensors are oriented differently in Symbian and Maemo devices, and we must account for this by using platform-specific code.</p>
<pre class="cpp"> <span class="type">bool</span> AccelerometerFilter<span class="operator">::</span>filter(<span class="type">QAccelerometerReading</span> <span class="operator">*</span>reading)
 {
     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> rx <span class="operator">=</span> reading<span class="operator">-</span><span class="operator">&gt;</span>x();
     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> ry <span class="operator">=</span> reading<span class="operator">-</span><span class="operator">&gt;</span>y();
     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> rz <span class="operator">=</span> reading<span class="operator">-</span><span class="operator">&gt;</span>z();

     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> divider <span class="operator">=</span> sqrt(rx <span class="operator">*</span> rx <span class="operator">+</span> ry <span class="operator">*</span> ry <span class="operator">+</span> rz <span class="operator">*</span> rz);

     <span class="comment">// Lowpass factor</span>
 <span class="preprocessor">#ifdef Q_OS_SYMBIAN</span>
     <span class="type">float</span> lowPassFactor <span class="operator">=</span> <span class="number">0.10</span>;
 <span class="preprocessor">#else</span>
     <span class="type">float</span> lowPassFactor <span class="operator">=</span> <span class="number">0.05</span>;
 <span class="preprocessor">#endif</span>

     <span class="comment">// Calculate the axis angles in degrees and reduce the noise in sensor</span>
     <span class="comment">// readings.</span>
     x <span class="operator">+</span><span class="operator">=</span> (acos(rx <span class="operator">/</span> divider) <span class="operator">*</span> RADIANS_TO_DEGREES <span class="operator">-</span> <span class="number">90</span> <span class="operator">-</span> x) <span class="operator">*</span> lowPassFactor;
     y <span class="operator">+</span><span class="operator">=</span> (acos(ry <span class="operator">/</span> divider) <span class="operator">*</span> RADIANS_TO_DEGREES <span class="operator">-</span> <span class="number">90</span> <span class="operator">-</span> y) <span class="operator">*</span> lowPassFactor;
     z <span class="operator">+</span><span class="operator">=</span> (acos(rz <span class="operator">/</span> divider) <span class="operator">*</span> RADIANS_TO_DEGREES <span class="operator">-</span> <span class="number">90</span> <span class="operator">-</span> z) <span class="operator">*</span> lowPassFactor;

     <span class="comment">// The orientations of the accelerometers are different between</span>
     <span class="comment">// Symbian and Maemo devices so we use the different axes</span>
     <span class="comment">// depending on the platform.</span>
 <span class="preprocessor">#if defined(Q_OS_SYMBIAN)</span>
     <span class="keyword">emit</span> rotationChanged(<span class="operator">-</span>y);
 <span class="preprocessor">#else</span>
     <span class="keyword">emit</span> rotationChanged(x);
 <span class="preprocessor">#endif</span>

     <span class="comment">// Don't store the reading in the sensor.</span>
     <span class="keyword">return</span> <span class="keyword">false</span>;
 }</pre>
<a name="the-qt-quick-ui"></a>
<h2>The Qt Quick UI</h2>
<p>BubbleLevel.qml is the main QML element. It represents the wooden board of the bubble level, and it also acts as a connection point between the QML and the Qt side. In the beginning of the element, there are two signals, two functions, and one property. All of these define the interface between Qt and QML.</p>
<p>On the Maemo platform, when the application is to be minimised, minimizeApplication is signalled. When a new calibration factor is to be stored in the device's memory, saveCorrectionAngle is signalled.</p>
<p>The handleRotation function acts as a Qt slot to which the AccelerometerFilters signal rotationChanged is connected. Similarly, the setCorrectionAngle function also acts as a Qt slot to which the Settings object's signal, correctionAngle, is connected.</p>
<p>The property alias taskSwitcherVisible is provided to allow the Qt model to show or hide the task switcher button which minimises the application. This is only meaningful on Maemo platforms, where every application normally has a task switcher button.</p>
<pre class="qml"> <span class="comment">// Signaled when task switcher button is pressed</span>
 signal <span class="type">minimizeApplication</span>()

 <span class="comment">// Signaled when correction angle is saved</span>
 signal <span class="type">saveCorrectionAngle</span>(variant angle)

 <span class="comment">// These functions are used as Qt slots</span>
 <span class="keyword">function</span> <span class="name">handleRotation</span>(<span class="name">deg</span>) {
     <span class="name">horTube</span>.<span class="name">rawangle</span> <span class="operator">=</span> <span class="name">deg</span>
 }

 <span class="keyword">function</span> <span class="name">setCorrectionAngle</span>(<span class="name">deg</span>) {
     <span class="name">horTube</span>.<span class="name">angleconstant</span> <span class="operator">=</span> <span class="name">deg</span>
 }

 <span class="comment">// Used to show the task switcher button in Maemo targets</span>
 property <span class="type">alias</span> <span class="name">taskSwitcherVisible</span>: <span class="name">taskSwitcher</span>.<span class="name">visible</span></pre>
<p>The Tube element represents the the glass tube of the bubble level. It is anchored to the centre of the wooden board. The width and height are calculated with specific factors to make the glass tube scale to different resolutions.</p>
<pre class="qml"> <span class="type">Tube</span> {
     <span class="name">id</span>: <span class="name">horTube</span>

     property <span class="type">real</span> <span class="name">rawangle</span>: <span class="number">0</span>
     property <span class="type">real</span> <span class="name">angleconstant</span>: <span class="number">0</span>

     <span class="name">anchors</span>.horizontalCenter: <span class="name">parent</span>.<span class="name">horizontalCenter</span>
     <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">*</span> <span class="number">0.775</span>; <span class="name">height</span>: <span class="name">parent</span>.<span class="name">height</span> <span class="operator">*</span> <span class="number">0.15625</span>
     <span class="name">deg</span>: <span class="name">rawangle</span> <span class="operator">-</span> <span class="name">angleconstant</span>
 }</pre>
<p>In the implementation of Tube.qml, the property deg represents the current inclination. The x-position of the bubble is bound to the JavaScript function calX which is called every time the property deg, center, or bubblCenter is changed. The function places the bubble in the corresponding place on its parent.</p>
<pre class="qml"> <span class="type"><a href="qml-item.html">Item</a></span> {
     <span class="name">id</span>: <span class="name">tube</span>

     property <span class="type">real</span> <span class="name">deg</span>

     <span class="type"><a href="qml-image.html">Image</a></span> {
         <span class="name">id</span>: <span class="name">bubble</span>

         property <span class="type">real</span> <span class="name">center</span>: <span class="name">tube</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>
         property <span class="type">real</span> <span class="name">bubbleCenter</span>: <span class="name">bubble</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>

         <span class="keyword">function</span> <span class="name">calX</span>() {
             var <span class="name">newX</span> = <span class="name">center</span> <span class="operator">+</span> <span class="name">tube</span>.<span class="name">deg</span> <span class="operator">/</span> -<span class="number">20</span> <span class="operator">*</span> <span class="name">center</span>

             <span class="keyword">if</span>((<span class="name">newX</span> <span class="operator">-</span> <span class="name">bubbleCenter</span>) <span class="operator">&lt;</span> <span class="number">0</span>) {
                 <span class="keyword">return</span> <span class="number">0</span>
             }
             else <span class="keyword">if</span>((<span class="name">newX</span> <span class="operator">+</span> <span class="name">bubbleCenter</span>) <span class="operator">&gt;</span> <span class="name">tube</span>.<span class="name">width</span>) {
                 <span class="keyword">return</span> <span class="name">tube</span>.<span class="name">width</span> <span class="operator">-</span> <span class="number">2</span> <span class="operator">*</span> <span class="name">bubbleCenter</span>
             }

             <span class="keyword">return</span> <span class="name">newX</span> <span class="operator">-</span> <span class="name">bubbleCenter</span>;
         }

         <span class="name">x</span>: <span class="name">calX</span>()
         <span class="name">width</span>: <span class="number">0.16129032</span> <span class="operator">*</span> <span class="name">parent</span>.<span class="name">width</span>; <span class="name">height</span>: <span class="number">0.66666667</span> <span class="operator">*</span> <span class="name">parent</span>.<span class="name">height</span>
         <span class="name">source</span>: <span class="string">&quot;images/bubble.png&quot;</span>
         <span class="name">smooth</span>: <span class="number">true</span>
     }

     <span class="type"><a href="qml-image.html">Image</a></span> {
         <span class="name">anchors</span>.horizontalCenter: <span class="name">parent</span>.<span class="name">horizontalCenter</span>
         <span class="name">width</span>: <span class="number">0.36451613</span> <span class="operator">*</span> <span class="name">parent</span>.<span class="name">width</span>; <span class="name">height</span>: <span class="number">0.66666667</span> <span class="operator">*</span> <span class="name">parent</span>.<span class="name">height</span>
         <span class="name">source</span>: <span class="string">&quot;images/scale.png&quot;</span>
     }

     <span class="type"><a href="qml-image.html">Image</a></span> {
         <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>; <span class="name">height</span>:  <span class="number">0.32</span> <span class="operator">*</span> <span class="name">parent</span>.<span class="name">height</span>
         <span class="name">opacity</span>: <span class="number">0.8</span>
         <span class="name">source</span>: <span class="string">&quot;images/reflection.png&quot;</span>
     }
 }</pre>
</div>
<!-- @@@demos/mobile/qtbubblelevel -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2015 The Qt Company Ltd.
      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>
      Qt and respective logos are trademarks of The Qt Company Ltd 
      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>