Sur cette page

Comment créer votre propre capteur

Le module « Qt Sensors » permet d'accéder au matériel des capteurs via des interfaces QML et C++.

L'API est prise en charge sur Android, iOS et Windows (MSVC).

Créer son propre capteur à l'aide de l'API C++

L'utilisation de l'API C++ d'un module Qt nécessite une liaison avec la bibliothèque du module, soit directement, soit via d'autres dépendances.

Création d'un capteur

Pour créer votre propre capteur, procédez comme suit :

  • Créez vos propres classes `MySensor` et `MySensorReading`
    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;
      };
  • Créez un MySensorBackend en héritant de QSensorBackend
  • Créez la classe d'usine MySensorBackendFactory pour instancier ce backend en héritant d'une classe QSensorBackendFactory
  • Enregistrez la classe d'instanciation du backend en appelant ` QSensorManager::registerBackend ("MySensorType", "MySensorId", &myfactory)`
  • Instanciez le nouveau MySensor et commencez à l'utiliser

Une autre option consiste à intégrer les capteurs dans un plugin de capteurs que vous pouvez utiliser à la demande.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.