Turning phone into magnetic compass by using QML and Qt Mobility
Article Metadata
Code Example
Article
This code snippet shows how we can convert our Magnetic North sensor enabled phone to a compass by using QML and Qt Mobility. The compass image is rotated around based on the physical alignment of phone with respect to north. There are two modules involved here. Qt mobility plug-ins for provides the angle and user interface shows the compass image by using QML. This example creates Qt plug-in for Sensor according to How to install and use Qt Quick extension plug-in in Symbian article.
How Qt Mobility emits angle changes
When the orientation of the device is changed we emit AngleChanged signal and update the property m_AnglefromNorth. The changes are detected by custom QML element.
if(m_AnglefromNorth != (int)(reading->azimuth()))
{
emit AngleChanged();
m_AnglefromNorth = reading->azimuth();
}
The custom QML element (Compass) is created with the following QML code:
import QtQuick 1.0
Rectangle {
id: compass
width: 200; height: 200; color: "gray"
property variant anglefromNorth
property variant calibrationlevel
Image{
id: compassimageid
source: "compass.png"
smooth: true
rotation : -anglefromNorth
}
}
Finally a Qt Quick project is created to show the image and rotation on UI:
import QtQuick 1.0
import com.nokia.qmlcompass 1.0
Compass { // this comes from Compass.qml
CompassImage { // this class is defined in C++ (plugin.cpp)
id: comimage
}
anglefromNorth: comimage.angle
calibrationlevel: comimage.calibration
MouseArea
{
anchors.fill: parent
// if you click on screen it will exit
onClicked:
{
Qt.quit();
}
}
}
Example Applications
Example application was tested with N8 compiled and linked with QTSDK 1.1 TP. Can be found from following link: File:QtQmlCompas.zip


Hamishwillee - Screenshot would be nice ...
A picture is worth a thousand words :-)hamishwillee 09:54, 10 September 2012 (EEST)