The QML Sensors Plugin provides an easy to use interface to the Sensors API. It enables us to receive sensor events and to read current values from sensors.
The plugin contains many sensor types and access functions to read values from them. As an example consider the orientation sensor. The orientation example simply displays text on-screen to show the current orientation.
The QML code that reads the value is quite simple. Here we see a QML component orientation declared which is an OrientationSensor element. First the sensor is started by setting the active property to true. The element receives a signal when the reading changes and it is picked up by the onReadingChanged slot. Now the reading property of this element can be used to extract the current orientation so that it can be compared against the defined values of various orientations in the OrientationReading element.
OrientationSensor { id: orientation active: true onReadingChanged: { if (reading.orientation == OrientationReading.FaceUp) content.state = "FaceUp"; // ... more tests for different orientations ... } }
Other sensors can be treated in a similar manner. For example, the Compass sensor could have almost identical coding
Compass { id: compass active: true onReadingChanged: { compassHeading.text = reading.azimuth; // ... } }
The Accelerometer element reports on linear acceleration along the X, Y and Z axes. |
|
The AmbientLightSensor element repors on ambient lighting conditions. |
|
The Compass element reports on heading using magnetic north as a reference. |
|
The Gyroscope element reports on rotational acceleration around the X, Y and Z axes. |
|
The LightSensor element reports on light levels using LUX. |
|
The Magnetometer element reports on magnetic field strength along the Z, Y and Z axes. |
|
The OrientationSensor element reports device orientation. |
|
The ProximitySensor element reports on object proximity. |
|
The RotationSensor element reports on device rotation around the X, Y and Z axes. |
|
The Sensor element serves as a base type for sensors. |
|
The TapSensor element reports tap and double tap events along the X, Y and Z axes. |
The AccelerometerReading element holds the most recent Accelerometer reading. |
|
The AmbientLightReading element holds the most AmbientLightSensor reading. |
|
The CompassReading element holds the most recent Compass reading. |
|
The GyroscopeReading element holds the most recent Gyroscope reading. |
|
The LightReading element holds the most recent LightSensor reading. |
|
The MagnetometerReading element holds the most recent Magnetometer reading. |
|
The OrientationReading element holds the most recent OrientationSensor reading. |
|
The ProximityReading element holds the most recent ProximitySensor reading. |
|
The RotationReading element holds the most recent RotationSensor reading. |
|
The SensorReading element serves as a base type for sensor readings. |
|
The TapReading element holds the most recent TapSensor reading. |
© 2008-2011 Nokia Corporation and/or its subsidiaries. Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide.
All other trademarks are property of their respective owners. Privacy Policy
Licensees holding valid Qt Commercial licenses may use this document in accordance with the Qt Commercial License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written agreement between you and Nokia.
Alternatively, this document may be used under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation.