Namespaces
Variants
Actions
(Difference between revisions)

QtMagnetometer Sensor Example Using QtMobility Project

Jump to: navigation, search
(Chintandave er - added articlemeta tag and Abstract)
m (Hamishwillee - Improve Abstract)
Line 1: Line 1:
[[Category:Qt]][[Category:Qt Mobility]][[Category:Code Examples]]
+
[[Category:Qt Mobility]][[Category:Code Examples]]
{{Abstract|This code snippet shows how to use QMagnetometer Sensor using Qt Mobility Project and display sensor data on the screen}}
+
{{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.}}
  
 
{{ArticleMetaData
 
{{ArticleMetaData
|sourcecode= [[Media: QtMagnetometer.zip]]<!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
+
|sourcecode= [[Media: QtMagnetometer.zip]]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= N8,C7,E7
 
|devices= N8,C7,E7
Line 10: Line 10:
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= QMagnetometer,Qt Mobility ,Sensor API
+
|keywords= QMagnetometer, Qt Mobility, Sensor API
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
|creationdate= 20101119<!-- Format YYYYMMDD -->
+
|creationdate= 20101119
|author= [[User:chintandave_er]] <!-- Display as link [[User:username]] -->
+
|author= [[User:chintandave_er]]
 
}}
 
}}
 
 
== Overview ==
 
 
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].
 
  
 
==Preconditions==
 
==Preconditions==
Line 59: Line 54:
 
     TARGET.EPOCHEAPSIZE = 0x020000 0x800000
 
     TARGET.EPOCHEAPSIZE = 0x020000 0x800000
 
}
 
}
 
 
 
</code>
 
</code>
 
 
  
 
==Header==
 
==Header==

Revision as of 04:14, 2 January 2012

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

Article Metadata

Code Example
Tested with
Devices(s): N8,C7,E7

Compatibility
Platform(s): Symbian

Article
Keywords: QMagnetometer, Qt Mobility, Sensor API
Created: chintandave_er (19 Nov 2010)
Last edited: hamishwillee (02 Jan 2012)

Contents

Preconditions

Read this for how to CS001615 - Setting up Qt Mobility

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
}

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;
};


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));
 
}
}

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

Example Applications

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

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)

110 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved