Using Qt sensors

Not all sensor types are available on all devices, so it's a good idea to verify what's available on a particular device. You can determine what sensors are available on a device by using the QSensor::sensorTypes() function. This function returns a list containing all the sensor types that are supported on the device.

Accessing Qt sensor data

How you access Qt sensor data is generally the same whether you're using QML or C++ in Cascades.

Requests from the app are delivered through  QSensor to QSensorBackend, which communicates with platform services and sensor hardware. Data is returned through the  QSensorReading class. The diagram to the right shows how these components interact with each other.

A diagram that shows how an app communicates with the device sensors.


To access sensor readings in your app, link the Sensors (libQtSensors) library by adding the following two lines to your .pro file:

CONFIG += mobility
MOBILITY += sensors

You must qualify the QtSensors namespace and the class name, and add a using directive for the QtMobility namespace.

// Import statement for sensors available in API levels
// 1.1 and earlier.
import QtMobility.sensors 1.2

// Import statement for sensors made available in API
// levels 1.2 and later.
import QtMobility.sensors 1.3
#include <QtSensors/QCompass>

using namespace QtMobility

Not applicable

Qt sensors sample

The sensor is started by setting the  active property to true. The component receives a signal when the reading changes, which it captures using the onReadingChanged() signal handler. Within the signal handler, you can access the reading  property of the element to extract the current sensor data so that your app can respond accordingly. Sensor objects must be attached to an existing VisualNode using the attachedObjects property.

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        layout: DockLayout {}
        Label {
            id: compassHeading
            text: "Sensors!"
        attachedObjects: [
            Compass {
                id: compass
                active: true
                onReadingChanged: {
                    compassHeading.text = reading.azimuth;

To handle sensor data in C++, the first thing you do is create an instance of QSensor or one of its subclasses on the stack or heap.

#include <QtSensors/QAccelerometer>
#include <QtSensors/QOrientationSensor>
using namespace QtMobility

// Create the sensor on the heap 
// (deleted when object is deleted).
QAccelerometer *sensor = new QAccelerometer(this);

// Create the sensor on the stack 
// (deleted when current scope ends).
QOrientationSensor orient_sensor;

Next, you start the sensor.


Then you receive the sensor reading using one of the QSensorReading classes and extract the data.

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

// For debugging purposes
qDebug() << "x acceleration:" << x;
qDebug() << "y acceleration:" << y;
qDebug() << "z acceleration:" << z;

If your app needs to be able to listen for changes in sensor readings, you can connect a custom slot to the QSensor::readingChanged() signal and handle the sensor data within the custom slot.

// SensorsApp.cpp
#include "SensorsApp.h"

#include <bb/cascades/Application>
#include <QtSensors/QCompass>

using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
    : QObject(app)
    // Define the UI and perform any other required operations

    // Create the compass sensor
    m_CompassSensor = new QCompass(this);

    // Set the orientation mode to fixed so that sensor 
    // readings aren't affected by device orientation

    bool res = connect(m_CompassSensor,



void SensorsApp::compassReadingChanged()
    QCompassReading *reading = m_CompassSensor->reading();

    qreal azimuth = reading->azimuth();
    qDebug() << "The azimuth is" << azimuth << "degrees.";

After you're finished with the sensor, all that's left to do is stop the sensor.


Here's the SensorsApp.h file:

// SensorsApp.h


#include <QObject>
#include <QtSensors/QCompass>


namespace bb { namespace cascades { class Application; }}

using namespace bb::cascades;

class SensorsApp : public QObject


    SensorsApp(bb::cascades::Application *app);
    virtual ~SensorsApp() {}

public slots:

void compassReadingChanged();


	QCompass *m_CompassSensor;

#endif /* SENSORSAPP_H_ */

Not applicable

Last modified: 2015-05-07

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus