(Difference between revisions)

Accelerometer Sensor Example Using QtMobility Project

From Nokia Developer Wiki
Jump to: navigation, search
kiran10182 (Talk | contribs)
m (removed from knowlegebase section)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Qt]][[Category:Qt for Maemo]][[Category:Qt for Symbian]][[Category:Qt Mobility]]
[[Category:Sensor]][[Category:Qt Mobility]][[Category:Code Examples]]
== Overview ==
{{Abstract|This code snippet shows how to use the [http://doc.qt.nokia.com/qtmobility-1.1.0/index.html Qt Mobilty] [http://doc.qt.nokia.com/qtmobility-1.1.0/qaccelerometer.html QAccelerometer] and  [http://doc.qt.nokia.com/qtmobility-1.1.0/sensors-api.html Sensor API]s to get and display sensor data.}}
This code snippet shows how to use [http://doc.qt.nokia.com/qtmobility-1.1.0/qaccelerometer.html QAccelerometer] Sensor using [http://labs.trolltech.com/page/Projects/QtMobility Qt Mobility Project] and display sensor data in the screen. This Example use [http://doc.qt.nokia.com/qtmobility-1.1.0/sensors-api.html Sensor API] of [http://doc.qt.nokia.com/qtmobility-1.1.0/index.html Qt Mobilty].
|sourcecode= [[Media: QtAccelerometer_QtMobility.zip]]<!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= N8,C7,E7
|platform= Symbian
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= QAccelerometer,Qt Mobility ,Sensor API
|id= <!-- Article Id (Knowledge base articles only) -->
|creationdate= 20101118<!-- Format YYYYMMDD -->
|author= [[User:chintandave_er]] <!-- Display as link [[User:username]] -->
Read this for how to [[CS001615 - Setting up Qt Mobility]]  
Read this for how to [[CS001615 - Setting up Qt Mobility]]  
Line 90: Line 102:
<code cpp>
<code cpp-qt>
#include "accelerometer.h"
#include "accelerometer.h"
Line 173: Line 185:
Here is the QtAccelerometer Application's Screenshot. You can change the Accelerometer Sensor data from Qt Simulator.
Here is the QtAccelerometer Application's Screenshot. You can change the Accelerometer Sensor data from Qt Simulator.
Line 185: Line 197:
==Related Articles==
==Related Articles==
[[Turning phone into magnetic compass using Qt mobility project]][[Category:Code Examples]]
* [[QtMagnetometer Sensor Example Using QtMobility Project]]
* [[Turning phone into magnetic compass using Qt mobility project]]
* [[Compass Sensor Example Using QtMobility Project]][[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 04:23, 11 October 2012

This code snippet shows how to use the Qt Mobilty QAccelerometer and Sensor APIs to get and display sensor data.

Article Metadata
Code ExampleTested with
Devices(s): N8,C7,E7
Platform(s): Symbian
Keywords: QAccelerometer,Qt Mobility ,Sensor API
Created: chintandave_er (18 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)


[edit] Preconditions

Read this for how to CS001615 - Setting up Qt Mobility

[edit] Project File (.pro file)

  • Add the Qt Mobility project configuration option in the .Pro file as shown below
CONFIG += mobility
MOBILITY += sensors
  • here is the full project(.pro) file.
QT       += core gui
TARGET = QtAccelerometer
SOURCES += main.cpp\
HEADERS += accelerometer.h
CONFIG += mobility
CONFIG += debug
MOBILITY = sensors
symbian {
TARGET.UID3 = 0xe239264e
TARGET.EPOCHEAPSIZE = 0x020000 0x800000

[edit] Header

Here is the accelerometer.h header file.

#include <QtGui/QMainWindow>
#include <QGraphicsView>
#include <QBasicTimer>
#include <QTimer>
#include <QAccelerometer> // Add Sensor Class
// add Qt Mobility Project Namespace
class QGraphicsRectItem;
class QGraphicsSimpleTextItem;
class Accelerometer : public QGraphicsView
Accelerometer(QWidget *parent = 0);
bool event(QEvent *event);
void updateXYZPosition();
QGraphicsRectItem *Rect;
QGraphicsSimpleTextItem *text;
QAccelerometer *accelerometerSensor;
QBasicTimer Timer;
QTimer inactiveTimer;

[edit] Source

#include "accelerometer.h"
#include <QGraphicsRectItem>
#include <QGraphicsSimpleTextItem>
#include <QResizeEvent>
#include <QTimerEvent> // timer event
#include <QDebug>
Accelerometer::Accelerometer(QWidget *parent) : QGraphicsView(parent), Rect(0), text(0), accelerometerSensor(0)
QGraphicsScene *scene = new QGraphicsScene(this);
Rect = new QGraphicsRectItem();
text = new QGraphicsSimpleTextItem(Rect);
accelerometerSensor = new QAccelerometer(this);
if (!Timer.isActive())
Timer.start(20, this);
// start the sensor
if (!accelerometerSensor->isActive())
if (!accelerometerSensor->isActive())
qDebug() << "accelerometer sensor didn't start!" << endl;
bool Accelerometer::event(QEvent *event)
switch (event->type()) {
case QEvent::Timer:
if (static_cast<QTimerEvent*>(event)->timerId() == Timer.timerId())
updateXYZPosition(); // update the xyz position
return QGraphicsView::event(event);
void Accelerometer::updateXYZPosition()
QAccelerometerReading *reading = accelerometerSensor->reading();
qreal xacceleration = 0.0f;
qreal yacceleration = 0.0f;
qreal zacceleration = 0.0f;
if (reading) {
xacceleration = reading->x();
yacceleration = reading->y();
zacceleration = reading->z();
// for debug purpose
qDebug() << "xacceleration x" << endl;
qDebug() << xacceleration << endl;
qDebug() << "yacceleration y" << endl;
qDebug() << yacceleration << endl;
qDebug() << "zacceleration z" << endl;
qDebug() << zacceleration << endl;
// set the text and display it on the screen
QLocale c(QLocale::C);
text->setText("x: " + c.toString(xacceleration) + " y: " + c.toString(yacceleration) + " z: " + c.toString(zacceleration));

[edit] Postconditions

Here is the QtAccelerometer Application's Screenshot. You can change the Accelerometer Sensor data from Qt Simulator.

QtAccelerometerSimulator.png QtAccelerometer.png

[edit] Example Applications

Example application can be found on the following link File:QtAccelerometer QtMobility.zip.

[edit] Related Articles

This page was last modified on 11 October 2012, at 04:23.
152 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.