How to use QCompass sensor
hamishwillee
(Talk | contribs) m (Remove Qt for Maemo and Qt for Symbian categories (only need specifier if there is some platform specific behaviour)) |
hamishwillee
(Talk | contribs) m (Text replace - "<code cpp>" to "<code cpp-qt>") |
||
| (10 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
| − | [[Category:Qt]][[Category:Qt | + | [[Category:Qt Mobility]][[Category:Code Examples]][[Category:MeeGo Harmattan]][[Category:Symbian]][[Category:Qt C++ UI]] |
| − | + | {{Abstract|This code snippet shows how to use Qt's [http://doc.qt.nokia.com/qtmobility-1.1.0/qcompass.html QCompass] sensor and display sensor data on the screen.}} | |
| − | + | {{ArticleMetaData <!-- v1.2 --> | |
| − | + | |sourcecode= [[Media: QtCompassSensorExample.zip]] [[Media:QtCompassSensorExample.zip]] | |
| − | == | + | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> |
| − | + | |devices= N8,C7,E7 | |
| − | + | |sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) --> | |
| + | |platform= Symbian | ||
| + | |devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) --> | ||
| + | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | ||
| + | |signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer --> | ||
| + | |capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. --> | ||
| + | |keywords= QCompass,Qt Mobility ,Sensor API | ||
| + | |language= <!-- Language category code for non-English topics - e.g. Lang-Chinese --> | ||
| + | |translated-by= <!-- [[User:XXXX]] --> | ||
| + | |translated-from-title= <!-- Title only --> | ||
| + | |translated-from-id= <!-- Id of translated revision --> | ||
| + | |review-by= <!-- After re-review: [[User:username]] --> | ||
| + | |review-timestamp= <!-- After re-review: YYYYMMDD --> | ||
| + | |update-by= <!-- After significant update: [[User:username]]--> | ||
| + | |update-timestamp= <!-- After significant update: YYYYMMDD --> | ||
| + | |creationdate= 20101129 | ||
| + | |author= [[User:Chintandave er]] | ||
| + | }} | ||
==Project File (.pro file)== | ==Project File (.pro file)== | ||
| − | * Add the Qt Mobility project configuration option in the .Pro file as shown below | + | * Add the Qt Mobility project configuration option in the '''.Pro''' file as shown below |
| − | <code> | + | <code cpp-qt> |
CONFIG += mobility | CONFIG += mobility | ||
MOBILITY += sensors | MOBILITY += sensors | ||
| Line 19: | Line 36: | ||
*here is the full project(.pro) file. | *here is the full project(.pro) file. | ||
| − | <code> | + | <code cpp-qt> |
QT += core gui | QT += core gui | ||
| Line 53: | Line 70: | ||
* Add the Qt Mobility namespace to use Qt Mobiltiy in header file | * Add the Qt Mobility namespace to use Qt Mobiltiy in header file | ||
| − | <code> | + | <code cpp-qt> |
QTM_USE_NAMESPACE | QTM_USE_NAMESPACE | ||
</code> | </code> | ||
| Line 92: | Line 109: | ||
QTimer inactiveTimer; | QTimer inactiveTimer; | ||
}; | }; | ||
| − | |||
| − | |||
</code> | </code> | ||
| Line 101: | Line 116: | ||
Here is the qtcompassensor.cpp source file. | Here is the qtcompassensor.cpp source file. | ||
| − | <code cpp> | + | <code cpp-qt> |
#include "qtcompassensor.h" | #include "qtcompassensor.h" | ||
| Line 136: | Line 151: | ||
if (!CompasSensor->isActive()) | if (!CompasSensor->isActive()) | ||
{ | { | ||
| − | qDebug() << "Compas Sensor | + | qDebug() << "Compas Sensor didn't start!" << endl; |
} | } | ||
| Line 185: | Line 200: | ||
==Related Articles== | ==Related Articles== | ||
| − | [[Turning phone into magnetic compass using Qt mobility project]] | + | * [[Turning phone into magnetic compass using Qt mobility project]] |
| − | + | * [[Accelerometer Sensor Example Using QtMobility Project]] | |
| − | [[Accelerometer Sensor Example Using QtMobility Project]] | + | * [[QtMagnetometer Sensor Example Using QtMobility Project]] |
| − | + | * [http://doc.qt.nokia.com/qtmobility-1.2/ Qt Mobility 1.2] | |
| − | [[QtMagnetometer Sensor Example Using QtMobility Project]] | + | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
Latest revision as of 04:13, 11 October 2012
This code snippet shows how to use Qt's QCompass sensor and display sensor data on the screen.
Article Metadata
Code Example
Tested with
Devices(s): N8,C7,E7
Compatibility
Platform(s): Symbian
Article
Keywords: QCompass,Qt Mobility ,Sensor API
Created: chintandave_er
(29 Nov 2010)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
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 = QtCompasSensor
TEMPLATE = app
SOURCES += main.cpp\
qtcompassensor.cpp
HEADERS += qtcompassensor.h
CONFIG += mobility
CONFIG += debug
MOBILITY = sensors
symbian {
TARGET.UID3 = 0xe00fb3c0
# TARGET.CAPABILITY +=
TARGET.EPOCSTACKSIZE = 0x14000
TARGET.EPOCHEAPSIZE = 0x020000 0x800000
}
Header
- Add the Qt Mobility namespace to use Qt Mobiltiy in header file
QTM_USE_NAMESPACE
Here is the qtcompassensor.h header file.
#include <QtGui/QMainWindow>
#include <QGraphicsView>
#include <QBasicTimer>
#include <QTimer>
#include <QCompass> // Add Sensor Class
// add Qt Mobility Project Namespace
QTM_USE_NAMESPACE
class QGraphicsRectItem;
class QGraphicsSimpleTextItem;
class QtCompasSensor : public QGraphicsView
{
Q_OBJECT
public:
QtCompasSensor(QWidget *parent = 0);
protected:
bool event(QEvent *event);
private:
void updateCompasSensor();
private:
QGraphicsRectItem *Rect;
QGraphicsSimpleTextItem *text;
QCompass *CompasSensor;
QBasicTimer Timer;
QTimer inactiveTimer;
};
Source
Here is the qtcompassensor.cpp source file.
#include "qtcompassensor.h"
#include <QGraphicsRectItem>
#include <QGraphicsSimpleTextItem>
#include <QResizeEvent>
// include timer event
#include <QTimerEvent>
#include <QDebug>
QtCompasSensor::QtCompasSensor(QWidget *parent)
: QGraphicsView(parent), Rect(0), text(0), CompasSensor(0)
{
QGraphicsScene *scene = new QGraphicsScene(this);
setScene(scene);
Rect = new QGraphicsRectItem();
text = new QGraphicsSimpleTextItem(Rect);
text->setBrush(Qt::gray);
scene->addItem(Rect);
CompasSensor = new QCompass(this);
inactiveTimer.setSingleShot(true);
if (!Timer.isActive())
Timer.start(20, this);
// start the sensor
if (!CompasSensor->isActive())
CompasSensor->start();
if (!CompasSensor->isActive())
{
qDebug() << "Compas Sensor didn't start!" << endl;
}
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
bool QtCompasSensor::event(QEvent *event)
{
switch (event->type()) {
case QEvent::Timer:
if (static_cast<QTimerEvent*>(event)->timerId() == Timer.timerId())
updateCompasSensor(); // update the sensor data
break;
default:
break;
}
return QGraphicsView::event(event);
}
void QtCompasSensor::updateCompasSensor()
{
QCompassReading *reading = CompasSensor->reading();
qreal azi = 0.0f;
qreal calibrationLevel = 0.0f;
if (reading) {
azi = reading->azimuth();
calibrationLevel = reading->calibrationLevel();
// set the text and display it on the screen
QLocale c(QLocale::C);
text->setText("azimuth: " + c.toString(azi) + "\n\nCompass Sensor Calibration Level: " + c.toString(calibrationLevel));
}
}
Postconditions
Here is the QtCompassSensor Application's Screenshot. You can change the Compass Sensor data like azimuth and Calibration Level from Qt Simulator.
Example Application
Example application can be found on the following link File:QtCompassSensorExample.zip.



