Qt Sensors - QML example

QtSensors - QML example demonstrates the QML sensors types in the QtSensors 5 import

Qt Sensors in QML

The QtSensors - QML example demonstrates the QML sensors types in the QtSensors 5 import.

To write a QML application that will use the QML sensors types in the QtSensors 5 import you need to to the following steps:

Import the QtSensors 5.x declarative plugin:

import QtSensors 5.0

Add the Sensor QML types into your qml file.

In this example we use the TiltSensor :

TiltSensor {
    id: tilt
    active: false
}

The Tilt-, AmbientLight- and the Proximity QML sensor types have the ‘enabled’ property in common. To start or stop the sensor set this property to true or false.

tilt.active = (tiltStart.text === "Start");

Reading the data can be done for each sensor type like following:

TiltSensor

text: "X Rotation: " + (tilt.reading ? tilt.reading.xRotation.toFixed(2) + "°" : "Unknown")
text: "Y Rotation: " + (tilt.reading ? tilt.reading.yRotation.toFixed(2) + "°" : "Unknown")

AmbientLightSensor

onReadingChanged: {
    if (reading.lightLevel == AmbientLightReading.Dark)
        ambientlighttext.text = "Ambient light: Dark";
    else if (reading.lightLevel == AmbientLightReading.Twilight)
        ambientlighttext.text = "Ambient light: Twilight";
    else if (reading.lightLevel == AmbientLightReading.Light)
        ambientlighttext.text = "Ambient light: Light";
    else if (reading.lightLevel == AmbientLightReading.Bright)
        ambientlighttext.text = "Ambient light: Bright";
    else if (reading.lightLevel == AmbientLightReading.Sunny)
        ambientlighttext.text = "Ambient light: Sunny";
    else
        ambientlighttext.text = "Ambient light: Unknown";
}

ProximitySensor

text: "Proximity: " +
      (proxi.active ? (proxi.reading.near ? "Near" : "Far") : "Unknown")

qmlqtsensors.qml Example File

ApplicationWindow.qml Example File

Button.qml Example File

Divider.qml Example File

main.cpp Example File

qmlqtsensors.pro Example File

qmlqtsensors.qrc Example File

button_background_disabled.png Image File

button_background_normal.png Image File

button_background_pressed.png Image File