How to create your own Sensor

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

The API is supported on Android , iOS ,和 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 : public QSensorReading
      {
        Q_OBJECT
        Q_PROPERTY(qreal myprop READ myprop)
        DECLARE_READING(MyReading)
    public:
        qreal myprop() const;
        void setMyprop(qreal myprop);
      };
    class MySensor : public QSensor
    {
        Q_OBJECT
    public:
        explicit MySensor(QObject *parent = 0);
        MyReading *reading() const;
        static char const * const sensorType;
      };
    							
  • 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 QSensorManager::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.