Code Examples Sensors API

From Qt Wiki
Jump to: navigation, search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

English Spanish


This page points to and provides quick overviews of existing code examples using the Sensors API, one of the suite of Qt Mobilty APIs.

Fall detector

The Fall Detector example from Forum Nokia uses the accelerometer in a mobile device to detect if the user has fallen, then uses the

  • the Contacts API to store and access an email address to be notified in the event of a fall
  • and the Messaging API to send an email notification with photo attachment.

Fall Dector Screen Shot Here's the code snippet for the fall detector using the Sensors API:

  1. include <QAccelerometer>

// Neccessary for Qt Mobility API usage QTM_USE_NAMESPACE

class AccelerationInfo : public QObject, public QAccelerometerFilter {



AccelerationInfo(QObject* parent = 0) : QObject(parent)

m_sensor = new QAccelerometer(this);

private slots:

// Override of QAcclerometerFilter::filter(QAccelerometerReading*)

void filter(QAccelerometerReading* reading)
qreal x = reading->x();
qreal y = reading->y();
qreal z = reading->z();

// Process acceleration sensor readings …

qDebug("Current device acceleration: x=%f y=%f z=%f", x, y, z);



QAccelerometer* m_sensor; };

Use accelerometer to control an Open GL-ES 3D model

This sensors tutorial from Mobile Qt-Entwicklung (Mobile Qt Development) shows the accelerometer in an N900 manipulating a 3D model created using Open GL. The descriptive text is in German, but all the code is available in a downloadable package and this demo video shows you how it works.

Here is the relevant accelerometer code:

GLWidget::GLWidget(QWidget *parent) :






xRot = 0;

yRot = 0;
zRot = 0;

_rotationSensorAvailable = false;

_rotationSensor = new QtMobility::QAccelerometer(this);
if (!_rotationSensor->isAvailable()) {
qWarning("No acceleration sensor available!");
} else {
_rotationSensorAvailable = true;
_rotationSensor->setSignalEnabled(false); // we get the values from the sensor itself
_rotationSensor->setUpdateInterval(100); // as quickly as possible

QTimer *timer = new QTimer(this);

QObject::connect(timer, SIGNAL (timeout()), this, SLOT (updateGL()));