The compass rose image is rotated according to the azimuth angle as reported by the sensor object. To avoid the implicit animation on the 0 <-> 360 degree switch, we disable the implicit animation for the 'rotationZ' property explicitly.

The Label shows the current azimuth angle as reported by the sensor object.

CompassSensor

The CompassSensor encapsulates the business logic of the application. It contains a QCompass object, which does the low-level communication with the compass sensor of the device, and provides a property 'azimuth' to make the retrieved sensor data available to the UI. It inherits from QCompassFilter and reimplements the 'bool filter(QCompassReading*)' method to retrieve the sensor data from the QCompass object.

Inside the constructor we try to connect the QCompass object to the hardware backend. If that's successful, we register the CompassSensor class as filter for the QCompass object and start the sensor to gather data.

CompassSensor::CompassSensor(QObject*parent)
: QObject(parent)
, m_azimuth(0)
{
// We'd like to lock to the initial orientation
OrientationSupport::instance()->setSupportedDisplayOrientation(SupportedDisplayOrientation::CurrentLocked);
// At first we have to connect to the sensor backend...if (!m_compassSensor.connectToBackend()) {
qWarning() <<"Cannot connect to compass sensor backend!";
}
// ... and then add a filter that will process the read data
m_compassSensor.addFilter(this);
// Do not report duplicated values
m_compassSensor.setSkipDuplicates(true);
// Start gathering the data
m_compassSensor.start();
}

The 'bool filter(QCompassReading*)' method is called whenever the QCompass object retrieved new data from the hardware sensor. Inside this method we update our internal azimuth property with the new value from the sensor and emit the change notification signal if the new value differs from the previous one.