×
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)
jimgilmour1 (Talk | contribs)
m (minor spelling)
Line 4: Line 4:
 
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].
 
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].
  
==PreCondition==
+
==Preconditions==
  
 
Read this for how to [[CS001615 - Setting up Qt Mobility]]  
 
Read this for how to [[CS001615 - Setting up Qt Mobility]]  
Line 169: Line 169:
 
</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.

Revision as of 23:33, 19 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

107 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.

×