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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
<?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/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>Qt Bubble Level Demo</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">"qrc:/qml/BubbleLevel.qml"</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">&</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"><</span><span class="type"><a href="qobject.html">QObject</a></span><span class="operator">*</span><span class="operator">></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">&</span>))<span class="operator">,</span>
<span class="operator">&</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">&</span>)));
<span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(<span class="operator">&</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">&</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">&</span>)));
<span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(<span class="operator">&</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">&</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">&</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">&</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">&</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">></span>setProperty(<span class="string">"taskSwitcherVisible"</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">></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 <QAccelerometerFilter></span>
<span class="preprocessor">#include <QVariant></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">&</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">></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">></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">></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"><</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">></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">"images/bubble.png"</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">"images/scale.png"</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">"images/reflection.png"</span>
}
}</pre>
</div>
<!-- @@@demos/mobile/qtbubblelevel -->
<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>
|