Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Turning phone into magnetic compass by using QML and Qt Mobility

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleCompatibility
Created: mahbub_s60 (28 Jan 2011)
Last edited: hamishwillee (11 Oct 2012)

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
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 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
anchors.fill: parent
// if you click on screen it will exit

Example Applications

Example application was tested with N8 compiled and linked with QTSDK 1.1 TP. Can be found from following link:

Related Article

This page was last modified on 11 October 2012, at 01:20.
48 page views in the last 30 days.