QtOnAndroid

Qt Sensors

Qt Signal-Slot Example | | Model-View Patterns with Qt

Access to sensors in Qt is done via deriving the QSensorFilter class and implementing its “filter” method, which is triggered whenever a sensor reading is received.

For example, the compass readings of the QCompass class are filtered by the corresponding QCompassFilter class, which is a convenience wrapper around the QSensorFilter class:

class CompassInfo: public QObject, public QCompassFilter
{
   Q_OBJECT

public:

   CompassInfo(QObject* parent = NULL)
      : QObject(parent), QCompassFilter(),
        m_azimuth(0)
   {
      m_sensor = new QCompass(this);
      m_sensor->addFilter(this);
      m_sensor->start();
   }

private:

   virtual bool filter(QCompassReading *reading)
   {
      qreal a = reading->azimuth();

      if (a != m_azimuth)
      {
         emit azimuth(a);
         m_azimuth = a;
      }

      return(true);
   }

private:

   QCompass* m_sensor;
   qreal m_azimuth;

signals:

   void azimuth(qreal a);
};

Full source code available here:

svn co svn://schorsch.efi.fh-nuernberg.de/qt-android/simplesensor


Qt Signal-Slot Example | | Model-View Patterns with Qt

Options: