Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

How to create your own Sensor

Steps for creating your own sensors

The Qt Sensors module provides access to sensor hardware via QML and C++ interfaces.

The API is supported on Android, iOS, and Windows (MSVC).

Creating your own sensor using C++ API

Using a Qt module’s C++ API requires linking against the module library, either directly or through other dependencies.

Creating a sensor

To create your own sensor you can use the following steps:

  • Create your own MySensor and MySensorReading classes

    class MyReadingPrivate():
    class MyReading(QSensorReading):
    
        Q_OBJECT
        Q_PROPERTY(qreal myprop READ myprop)
        DECLARE_READING(MyReading)
    # public
        myprop = qreal()
        def setMyprop(myprop):
    
    class MySensor(QSensor):
    
        Q_OBJECT
    # public
        MySensor = explicit(QObject parent = 0)
        reading = MyReading()
        * = char()
    
  • Create a MySensorBackend by inheriting from QSensorBackend

  • Create MySensorBackendFactory factory class for instantiating that backend by inheriting a class QSensorBackendFactory

  • Register the backend factory by calling registerBackend (“MySensorType”, “MySensorId”, &myfactory)

  • Instantiate the new MySensor and start using it

As an another option the sensors are put into a Creating a sensor plugin that you can use on demand.