Overview
To write a QML application that will use the QML sensors elements 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 elements into your qml file.
In this example we use the TiltSensor:
TiltSensor {
id: tilt
unit: TiltSensor.Degrees
enabled: false
accuracy: 1.0
speed: TiltSensor.Slow
}
The Tilt-, AmbientLight- and the Proximity QML element sensor have the 'enabled' property in common. To start or stop the sensor set this property to true or false.
tilt.enabled = (tiltStart.text === "Start" ? true: false);
Reading the data can be done for each sensor type like following:
TiltSensor
text: "X Rotation: " + tilt.xRotation + (tilt.unit === TiltSensor.Radians ? " rad" : "°")
text: "Y Rotation: " + tilt.yRotation + (tilt.unit === TiltSensor.Radians ? " rad" : "°")
AmbientLightSensor
onLightLevelChanged:{
if (ambientlight.lightLevel == AmbientLightSensor.Unknown)
ambientlighttext.text = "Ambient light: Unknown";
else if (ambientlight.lightLevel == AmbientLightSensor.Dark)
ambientlighttext.text = "Ambient light: Dark";
else if (ambientlight.lightLevel == AmbientLightSensor.Twilight)
ambientlighttext.text = "Ambient light: Twilight";
else if (ambientlight.lightLevel == AmbientLightSensor.Light)
ambientlighttext.text = "Ambient light: Light";
else if (ambientlight.lightLevel == AmbientLightSensor.Bright)
ambientlighttext.text = "Ambient light: Bright";
else if (ambientlight.lightLevel == AmbientLightSensor.Sunny)
ambientlighttext.text = "Ambient light: Sunny";
}
ProximitySensor
text: "Proximity: " + (proxi.near ? "near" : "far")
Files: