Namespaces

Variants
Actions

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 over the next few weeks. 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
Platform(s):
Symbian
Article
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
 
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


Related Article

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

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×