Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Qt Sensors C++ Overview#
Explains how to use the Qt Sensors C++ API.
Sensor Types#
On a device there can be many types of sensors. Not all of the types that the Qt Sensors API supports may be available. There may also be types available that are not defined in the Qt Sensors API. The types of sensors available on a device is found using the sensorTypes() function.
For a list of built-in sensor types, see the Sensor Classes section below.
Common Conventions#
Unless specified otherwise, Qt Sensors uses the Right Hand Cartesian coordinate system .
To allow for measurements in all 6 directions, negative values are used.
Where rotation around an axis is used, the rotation shall be expressed as a Right Hand rotation.
In general, sensor data is oriented relative to nativeOrientation , that is to the top of the device when the device is held in its natural orientation (normally when the device logo appears the right side up). If values are to be displayed on the screen, the values may need to be transformed so that they match the user interface orientation. A sensor may define its data as being oriented to the UI. This will be noted in the documentation for the sensor.
Using a Sensor#
The life cycle of a QSensor is typically:
Create an instance of
QSensoror one of its sub-classes on the stack or heap.Setup as required by the application.
Start receiving values.
Sensor data is used by the application.
Stop receiving values.
Here is an example of creating a sensor on the heap and on the stack.
# On the heap (deleted when this object is deleted) sensor = QAccelerometer(self) # On the stack (deleted when the current scope ends) orient_sensor = QOrientationSensor()
Accessing Sensor Data in a Generic Fashion#
The preferred way to deal with sensor data is via the Reading Classes . However, sometimes this may not be possible. For example, you may be deploying an application to a device that has a new sensor type but no C++ header describing the reading class is available.
Thanks to Qt’s property system you can still access the sensor data. You need to know 3 pieces of information in order to do this:
The sensor type.
The property name or index.
The property type or a comparable type.
For example, here is an example of how you can access a property of the accelerometer. This code does not require any compile-time links to QAccelerometer or QAccelerometerReading .
# start the sensor sensor = QSensor("QAccelerometer") sensor.start() # later reading = sensor.reading() x = reading.property("x").value<qreal>() y = reading.value(1).value<qreal>()
You can discover all of this information at runtime too.
Discovering Sensors And Reading Properties At Runtime#
Sometimes it may be that the available sensors are not known at development time. It is possible to find out which sensors are available as illustrated below:
mySensorList = QList() for type in QSensor.sensorTypes(): print("Found a sensor type:", type) for identifier in QSensor.sensorsForType(type): print(" ", "Found a sensor of that type:", identifier) QSensor* sensor = QSensor(type, self) sensor.setIdentifier(identifier) mySensorList.append(sensor)
Furthermore it is possible to discover the reading details for these sensors, as illustrated below:
for sensor in mySensorList: firstProperty = QSensorReading.staticMetaObject.propertyOffset() # Connect to backend first in case start() hasn't been called yet if not sensor.connectToBackend(): continue print("Sensor", sensor.identifier(), "reading properties:") reading = sensor.reading() if reading: mo = reading.metaObject() for i in range(firstProperty, mo.propertyCount()): name = mo.property(i).name() print(" ", name, reading.property(name).toByteArray())
Front End, Back End#
The Qt Sensors API has a front end, for application developers to use and a back end, where device implementors write code to access their hardware. As an application developer you do not need to access the back end though it may be useful to understand how it works.
Commands from the application are delivered through QSensor and then down to the device plugin. Data comes back through the QSensorReading class.
More information about the back end can be found in Qt Sensors Backend .
Main Classes#
The primary classes that make up the Qt Sensors API.
PySide6.QtSensors.QSensorThe QSensor class represents a single hardware sensor.
PySide6.QtSensors.QSensorFilterThe QSensorFilter class provides an efficient callback facility for asynchronous notifications of sensor changes.
PySide6.QtSensors.QSensorReadingThe QSensorReading class holds the readings from the sensor.
Reading Classes#
The best way to access sensor data is via one of these classes.
PySide6.QtSensors.QAccelerometerReadingThe QAccelerometerReading class reports on linear acceleration along the X, Y and Z axes.
PySide6.QtSensors.QAmbientLightReadingThe QAmbientLightReading class represents one reading from the ambient light sensor.
PySide6.QtSensors.QAmbientTemperatureReadingThe QAmbientTemperatureReading class holds readings of the ambient temperature.
PySide6.QtSensors.QCompassReadingThe QCompassReading class represents one reading from a compass.
PySide6.QtSensors.QGyroscopeReadingThe QGyroscopeReading class represents one reading from the gyroscope sensor.
PySide6.QtSensors.QHumidityReadingThe QHumidityReading class holds readings from the humidity sensor.
The QIRProximityReading class holds readings from the IR proximity sensor.
The QLidReading class holds readings from the Lid sensor.
PySide6.QtSensors.QLightReadingThe QLightReading class represents one reading from the light sensor.
PySide6.QtSensors.QMagnetometerReadingThe QMagnetometerReading class represents one reading from the magnetometer.
PySide6.QtSensors.QOrientationReadingThe QOrientationReading class represents one reading from the orientation sensor.
PySide6.QtSensors.QPressureReadingThe QPressureReading class holds readings from the pressure sensor.
PySide6.QtSensors.QProximityReadingThe QProximityReading class represents one reading from the proximity sensor.
PySide6.QtSensors.QRotationReadingThe QRotationReading class represents one reading from the rotation sensor.
The QTapReading class represents one reading from the tap sensor.
PySide6.QtSensors.QTiltReadingThe QTiltReading class holds readings from the tilt sensor.
Sensor Classes#
These classes provide convenience wrappers that reduce the need for casting. Each of these classes represents a sensor type that the Qt Sensors API knows about. Note that additional types may be made available at run-time. See Sensor Types for more information.
PySide6.QtSensors.QAccelerometerThe QAccelerometer class is a convenience wrapper around QSensor.
PySide6.QtSensors.QAmbientLightSensorThe QAmbientLightSensor class is a convenience wrapper around QSensor.
PySide6.QtSensors.QAmbientTemperatureSensorThe QAmbientTemperatureSensor class is a convenience wrapper around QSensor.
PySide6.QtSensors.QCompassThe QCompass class is a convenience wrapper around QSensor.
PySide6.QtSensors.QGyroscopeThe QGyroscope class is a convenience wrapper around QSensor.
PySide6.QtSensors.QHumiditySensorThe QHumiditySensor class is a convenience wrapper around QSensor.
The QIRProximitySensor class is a convenience wrapper around QSensor.
The QLidSensor class is a convenience wrapper around QSensor.
PySide6.QtSensors.QLightSensorThe QLightSensor class is a convenience wrapper around QSensor.
PySide6.QtSensors.QMagnetometerThe QMagnetometer class is a convenience wrapper around QSensor.
PySide6.QtSensors.QOrientationSensorThe QOrientationSensor class is a convenience wrapper around QSensor.
PySide6.QtSensors.QPressureSensorThe QPressureSensor class is a convenience wrapper around QSensor.
PySide6.QtSensors.QProximitySensorThe QProximitySensor class is a convenience wrapper around QSensor.
PySide6.QtSensors.QRotationSensorThe QRotationSensor class is a convenience wrapper around QSensor.
The QTapSensor class is a convenience wrapper around QSensor.
PySide6.QtSensors.QTiltSensorThe QTiltSensor class is a convenience wrapper around QSensor.
Filter Classes#
As with the sensor classes, these provide convenience wrappers that reduce the need for casting.
PySide6.QtSensors.QAccelerometerFilterThe QAccelerometerFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QAmbientLightFilterThe QAmbientLightFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QAmbientTemperatureFilterThe QAmbientTemperatureFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QCompassFilterThe QCompassFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QGyroscopeFilterThe QGyroscopeFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QHumidityFilterThe QHumidityFilter class is a convenience wrapper around QSensorFilter.
The QIRProximityFilter class is a convenience wrapper around QSensorFilter.
The QLidFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QLightFilterThe QLightFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QMagnetometerFilterThe QMagnetometerFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QOrientationFilterThe QOrientationFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QPressureFilterThe QPressureFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QProximityFilterThe QProximityFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QRotationFilterThe QRotationFilter class is a convenience wrapper around QSensorFilter.
The QTapFilter class is a convenience wrapper around QSensorFilter.
PySide6.QtSensors.QTiltFilterThe QTiltFilter class is a convenience wrapper around QSensorFilter.