×
Namespaces

Variants
Actions
(Difference between revisions)

QtMagnetometer Sensor Example Using QtMobility Project

From Nokia Developer Wiki
Jump to: navigation, search
jimgilmour1 (Talk | contribs)
m (minor spelling)
chintandave_er (Talk | contribs)
m (added article link in related link)
Line 183: Line 183:
 
[[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]][[Category:Code Examples]]

Revision as of 20:42, 29 November 2010

Contents

Overview

This code snippet shows how to use QMagnetometer Sensor using Qt Mobility Project and display sensor data on the screen. This Example use Sensor API of Qt Mobilty.

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

101 page views in the last 30 days.
×