QtMagnetometer Sensor Example Using QtMobility Project
(Chintandave er - added articlemeta tag and Abstract) |
hamishwillee
(Talk | contribs) m (Text replace - "<code cpp>" to "<code cpp-qt>") |
||
| (3 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
| − | + | [[Category:Qt Mobility]][[Category:Code Examples]] | |
| − | {{Abstract|This code snippet shows how to use QMagnetometer | + | {{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]] | + | |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 | + | |creationdate= 20101119 |
| − | |author= [[User:chintandave_er]] | + | |author= [[User:chintandave_er]] |
}} | }} | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
==Preconditions== | ==Preconditions== | ||
| Line 59: | Line 54: | ||
TARGET.EPOCHEAPSIZE = 0x020000 0x800000 | TARGET.EPOCHEAPSIZE = 0x020000 0x800000 | ||
} | } | ||
| − | |||
| − | |||
</code> | </code> | ||
| − | |||
| − | |||
==Header== | ==Header== | ||
| Line 108: | Line 99: | ||
==Source== | ==Source== | ||
| − | <code cpp> | + | <code cpp-qt> |
#include "magnetometer.h" | #include "magnetometer.h" | ||
#include <QGraphicsRectItem> | #include <QGraphicsRectItem> | ||
| Line 205: | Line 196: | ||
| − | -- [[User:Chintandave_er|Chintandave_er]] 05:40, 9 December 2010 (UTC) | + | -- [[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 Example
Source file: Media: QtMagnetometer.zip
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
(11 Oct 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.
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)



