×
Namespaces

Variants
Actions
(Difference between revisions)

QtMagnetometer Sensor Example Using QtMobility Project

From Nokia Developer Wiki
Jump to: navigation, search
chintandave_er (Talk | contribs)
m (minor formatting)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Qt]][[Category:Qt for Maemo]][[Category:Qt for Symbian]][[Category:Qt Mobility]]
+
[[Category:Qt Mobility]][[Category:Code Examples]]
== Overview ==
+
{{Abstract|This code snippet shows how to use [http://doc.qt.nokia.com/qtmobility-1.1.0/index.html Qt Mobilty]'s [http://doc.qt.nokia.com/qtmobility-1.1.0/qmagnetometer.html QMagnetometer] sensor.}}
  
This code snippet shows how to use [http://doc.qt.nokia.com/qtmobility-1.1.0/qmagnetometer.html QMagnetometer] Sensor using [http://labs.trolltech.com/page/Projects/QtMobility Qt Mobility Project] and display sensor data on the screen. This Example use [http://doc.qt.nokia.com/qtmobility-1.1.0/sensors-api.html Sensor API] of  [http://doc.qt.nokia.com/qtmobility-1.1.0/index.html Qt Mobilty].
+
{{ArticleMetaData
 +
|sourcecode= [[Media: QtMagnetometer.zip]]
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= N8,C7,E7
 +
|platform= Symbian
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= QMagnetometer, Qt Mobility, Sensor API
 +
|id= <!-- Article Id (Knowledge base articles only) -->
 +
|creationdate= 20101119
 +
|author= [[User:chintandave_er]]
 +
}}
  
==PreCondition==
+
==Preconditions==
  
 
Read this for how to [[CS001615 - Setting up Qt Mobility]]  
 
Read this for how to [[CS001615 - Setting up Qt Mobility]]  
Line 42: Line 54:
 
     TARGET.EPOCHEAPSIZE = 0x020000 0x800000
 
     TARGET.EPOCHEAPSIZE = 0x020000 0x800000
 
}
 
}
 
 
 
</code>
 
</code>
 
 
  
 
==Header==
 
==Header==
Line 91: Line 99:
 
==Source==
 
==Source==
  
<code cpp>
+
<code cpp-qt>
 
#include "magnetometer.h"
 
#include "magnetometer.h"
 
#include <QGraphicsRectItem>
 
#include <QGraphicsRectItem>
Line 169: Line 177:
 
</code>
 
</code>
  
==PostCondition==
+
==Postconditions==
  
 
Here is the QtMagnetometer Application's Screenshot. You can change the Magnetometer Sensor data like X , Y , Z Direction and Calibration Level from Qt Simulator.
 
Here is the QtMagnetometer Application's Screenshot. You can change the Magnetometer Sensor data like X , Y , Z Direction and Calibration Level from Qt Simulator.
Line 183: Line 191:
 
[[Turning phone into magnetic compass using Qt mobility project]]
 
[[Turning phone into magnetic compass using Qt mobility project]]
  
[[Accelerometer Sensor Example Using QtMobility Project]][[Category:Code Examples]]
+
[[Accelerometer Sensor Example Using QtMobility Project]]
 +
 
 +
[[Compass Sensor Example Using QtMobility Project]]
 +
 
 +
 
 +
--  [[User:Chintandave_er|Chintandave_er]] 05:40, 9 December 2010 (UTC)[[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 04:23, 11 October 2012

This code snippet shows how to use Qt Mobilty's QMagnetometer sensor.

Article Metadata
Code ExampleTested with
Devices(s): N8,C7,E7
Compatibility
Platform(s): Symbian
Symbian
Article
Keywords: QMagnetometer, Qt Mobility, Sensor API
Created: chintandave_er (19 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Contents

[edit] Preconditions

Read this for how to CS001615 - Setting up Qt Mobility

[edit] Project File (.pro file)

  • Add the Qt Mobility project configuration option in the .Pro file as shown below
CONFIG += mobility
MOBILITY += sensors
  • here is the full project(.pro) file.
QT       += core gui
 
TARGET = QtMagnetometer
TEMPLATE = app
 
 
SOURCES += main.cpp\
magnetometer.cpp
 
HEADERS += magnetometer.h
 
CONFIG += mobility
CONFIG += debug
MOBILITY = sensors
 
symbian {
TARGET.UID3 = 0xe26021e7
# TARGET.CAPABILITY +=
TARGET.EPOCSTACKSIZE = 0x14000
TARGET.EPOCHEAPSIZE = 0x020000 0x800000
}

[edit] Header

Here is the magnetometer.h header file.

#include <QtGui/QMainWindow>
#include <QGraphicsView>
#include <QBasicTimer>
#include <QTimer>
#include <QMagnetometer> // Add Sensor Class
 
// add Qt Mobility Project Namespace
QTM_USE_NAMESPACE
 
class QGraphicsRectItem;
class QGraphicsSimpleTextItem;
 
 
class Magnetometer : public QGraphicsView
{
Q_OBJECT
 
public:
Magnetometer(QWidget *parent = 0);
 
protected:
bool event(QEvent *event);
 
private:
void updateMagnetometerSensor();
 
private:
QGraphicsRectItem *Rect;
QGraphicsSimpleTextItem *text;
QMagnetometer *MagnetometerSensor;
QBasicTimer Timer;
QTimer inactiveTimer;
};


[edit] Source

#include "magnetometer.h"
#include <QGraphicsRectItem>
#include <QGraphicsSimpleTextItem>
#include <QResizeEvent>
 
// include timer event
#include <QTimerEvent>
#include <QDebug>
 
Magnetometer::Magnetometer(QWidget *parent) : QGraphicsView(parent), Rect(0), text(0), MagnetometerSensor(0)
{
QGraphicsScene *scene = new QGraphicsScene(this);
setScene(scene);
 
Rect = new QGraphicsRectItem();
 
text = new QGraphicsSimpleTextItem(Rect);
text->setBrush(Qt::gray);
scene->addItem(Rect);
 
MagnetometerSensor = new QMagnetometer(this);
inactiveTimer.setSingleShot(true);
 
if (!Timer.isActive())
Timer.start(20, this);
 
// start the sensor
if (!MagnetometerSensor->isActive())
MagnetometerSensor->start();
 
if (!MagnetometerSensor->isActive())
{
qDebug() << "Magnetometer sensor didn't start!" << endl;
}
 
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
 
bool Magnetometer::event(QEvent *event)
{
switch (event->type()) {
case QEvent::Timer:
if (static_cast<QTimerEvent*>(event)->timerId() == Timer.timerId())
updateMagnetometerSensor(); // update the sensor data
break;
default:
break;
}
return QGraphicsView::event(event);
}
 
void Magnetometer::updateMagnetometerSensor()
{
QMagnetometerReading *reading = MagnetometerSensor->reading();
qreal x = 0.0f;
qreal y = 0.0f;
qreal z = 0.0f;
qreal calibrationLevel = 0.0f;
if (reading) {
x = reading->x();
y = reading->y();
z = reading->z();
calibrationLevel = reading->calibrationLevel();
 
// set the text and display it on the screen
QLocale c(QLocale::C);
text->setText("x: " + c.toString(x) + " y: " + c.toString(y) + " z: " + c.toString(z) + "\n\nMagnetometer Calibration Level: " +
 
c.toString(calibrationLevel));
 
}
}

[edit] Postconditions

Here is the QtMagnetometer Application's Screenshot. You can change the Magnetometer Sensor data like X , Y , Z Direction and Calibration Level from Qt Simulator.

QtMagnetometerSimulator.png QtMagnetometer.png

[edit] Example Applications

Example application can be found on the following link File:QtMagnetometer.zip.

[edit] Related Articles

Turning phone into magnetic compass using Qt mobility project

Accelerometer Sensor Example Using QtMobility Project

Compass Sensor Example Using QtMobility Project


-- chintandave_er 05:40, 9 December 2010 (UTC)

This page was last modified on 11 October 2012, at 04:23.
102 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.

×