×
Namespaces

Variants
Actions
(Difference between revisions)

Accelerometer Sensor Example Using QtMobility Project

From Nokia Developer Wiki
Jump to: navigation, search
chintandave_er (Talk | contribs)
(Accelerometer Sensor Example Using QtMobility Project)
 
chintandave_er (Talk | contribs)
m (Overview Edited)
Line 1: Line 1:
 
[[Category:Qt]][[Category:Qt for Maemo]][[Category:Qt for Symbian]][[Category:Qt Mobility]]
 
[[Category:Qt]][[Category:Qt for Maemo]][[Category:Qt for Symbian]][[Category:Qt Mobility]]
 
{{KBCS}}
 
{{KBCS}}
[[Category:Code Examples]]
+
 
  
 
== Overview ==
 
== Overview ==
  
This code snippet shows how to use "QAccelerometer" Sensor using [http://labs.trolltech.com/page/Projects/QtMobility Qt Mobility Project] and display sensor data in the screen.
+
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].
  
 
==PreCondition==
 
==PreCondition==
Line 188: Line 188:
 
==Related Articles==
 
==Related Articles==
  
[[Turning phone into magnetic compass using Qt mobility project]]
+
[[Turning phone into magnetic compass using Qt mobility project]][[Category:Code Examples]]

Revision as of 21:26, 18 November 2010

Template:KBCS


Contents

Overview

This code snippet shows how to use QAccelerometer Sensor using Qt Mobility Project and display sensor data in the screen. This Example use Sensor API of Qt Mobilty.

PreCondition

Read this for how to CS001615 - Setting up Qt Mobility

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
TEMPLATE = app
 
 
SOURCES += main.cpp\
accelerometer.cpp
 
HEADERS += accelerometer.h
 
CONFIG += mobility
CONFIG += debug
MOBILITY = sensors
 
symbian {
TARGET.UID3 = 0xe239264e
# TARGET.CAPABILITY +=
TARGET.EPOCSTACKSIZE = 0x14000
TARGET.EPOCHEAPSIZE = 0x020000 0x800000
}


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
QTM_USE_NAMESPACE
 
class QGraphicsRectItem;
class QGraphicsSimpleTextItem;
 
 
class Accelerometer : public QGraphicsView
{
Q_OBJECT
 
public:
Accelerometer(QWidget *parent = 0);
 
protected:
bool event(QEvent *event);
 
private:
void updateXYZPosition();
 
private:
QGraphicsRectItem *Rect;
QGraphicsSimpleTextItem *text;
QAccelerometer *accelerometerSensor;
QBasicTimer Timer;
QTimer inactiveTimer;
};


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);
setScene(scene);
 
Rect = new QGraphicsRectItem();
 
text = new QGraphicsSimpleTextItem(Rect);
text->setBrush(Qt::gray);
scene->addItem(Rect);
 
accelerometerSensor = new QAccelerometer(this);
inactiveTimer.setSingleShot(true);
 
if (!Timer.isActive())
Timer.start(20, this);
 
// start the sensor
if (!accelerometerSensor->isActive())
accelerometerSensor->start();
 
if (!accelerometerSensor->isActive())
{
qDebug() << "accelerometer sensor didn't start!" << endl;
}
 
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
 
bool Accelerometer::event(QEvent *event)
{
switch (event->type()) {
case QEvent::Timer:
if (static_cast<QTimerEvent*>(event)->timerId() == Timer.timerId())
updateXYZPosition(); // update the xyz position
break;
default:
break;
}
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));
 
}
}

PostCondition

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

QtAccelerometerSimulator.png QtAccelerometer.png

Example Applications

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

Related Articles

Turning phone into magnetic compass using Qt mobility project

141 page views in the last 30 days.