Qt gravity sensing
Today we will introduce the gravity sensing function of QT. We will write a small program to detect the X, Y, and Z values of gravity sensing in real time and display them on the screen, finally, we put it in my Nokia 5230 mobile phone to run:
Strongly recommended reference:Http://apidocs.meego.com/1.1/core/html/qtmobility/sensors-api.html
In addition, you can also refer to (this article involves many aspects ):
Http://www.developer.nokia.com/Community/Wiki/Qt_Mobility_example_application:_Fall_Detector
Paste several images at a Glance:
1.
First, create a new "qt gui application" and enter mysensor:
2.
Select "Saipan device ":
3.
Select "qwidget ":
Created!
4.
Modify the widget. UI as follows. We have added a Pushbutton with the ID btnstart. Below is a plain text edit with the ID textdata. When you press btnstart for the first time, the title changes to pause. At the same time, the X, Y, and Z data of gravity sensing are recorded and displayed in textdata and transmitted to the PC at the same time. When you press btnstart again, the record is paused.
5.
Qt sensors include gravity, light, and direction. When using a sensor, we should first define a sensor object, and then define a class inherited from qsensorfilter to implement the filter method (this method is defined as virtual in qsensorfilter ), this function is called whenever the sensor data changes. Finally, we can connect the Sensor Object with this filter.
It should be noted that each sensor corresponds to a class, for example, the qaccelerometer class is a gravity sensor class, And the qambientlightsensor class is a light sensor class. However, all sensor classes are inherited from a common parent class: qsensor. Therefore, when we use a sensor class, a simple method is to use a dedicated sensor class instead of a qsensor class. In addition, the filter is the same. All sensor filters inherit from a common parent class qsensorfilter. Therefore, our custom filter class should inherit from the dedicated filter class, instead of the qsensorfilter class.
The principle is very simple. Let's get started:
1.
First, we define a filter class that inherits from qaccelerometerfilter and implements the filter method. Add a myfilter class for the project as follows:
2.
Modify myfilter. h as follows:
#ifndef MYFILTER_H#define MYFILTER_H#include <QPlainTextEdit>#include <QAccelerometerFilter>/* Neccessary for Qt Mobility API usage */QTM_USE_NAMESPACEclass MyFilter : public QAccelerometerFilter{public: MyFilter(QPlainTextEdit * text);private: QPlainTextEdit * text;protected: bool filter(QAccelerometerReading * reading);};#endif // MYFILTER_H
Note: qtm_use_namespace indicates that the QT mobility namespace is used. The sensor belongs to the QT mobility API, so we need to add this sentence. In addition, we also added the filter method declaration. Text is the plain text edit on the main interface, so that we can modify its content in the filter.
3.
Modify myfilter. cpp as follows:
#include <QDebug>#include <QAccelerometerReading>#include "myfilter.h"MyFilter::MyFilter(QPlainTextEdit * t){ text = t;}bool MyFilter::filter(QAccelerometerReading *reading){ qreal x = reading->x(); qreal y = reading->property("y").value<qreal>(); qreal z = reading->value(2).value<qreal>(); qDebug() << "x = " << x << ", y = " << y << ", z = " << z; QString str; str.sprintf("x: %f\ny: %f\nz: %f", x, y, z); text->setPlainText(str);}
Note: In the filter method, we use three methods to obtain data. The first method is the fastest, the third method is the second, and the second method is the slowest. The value of the third method is 0, 1 for X, Y, and Z, 2. Order Number. Qdebug can send data directly to the PC, and we also display the data in plain text edit.
4.
Modify widget. h as follows:
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QAccelerometer>#include "myfilter.h"/* Neccessary for Qt Mobility API usage */QTM_USE_NAMESPACEnamespace Ui { class Widget;}class Widget : public QWidget{ Q_OBJECTpublic: explicit Widget(QWidget *parent = 0); ~Widget();private: Ui::Widget *ui; QAccelerometer * sensor; MyFilter * filter; bool isRunning;private slots: void btnPressed();};#endif // WIDGET_H
5.
Modify widget. cpp as follows:
#include "widget.h"#include "ui_widget.h"#include "myfilter.h"Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); QObject::connect( ui->btnStart, SIGNAL(clicked()), this, SLOT(btnPressed())); isRunning = false; sensor = new QAccelerometer(this); filter = new MyFilter(ui->textData); sensor->addFilter(filter);}Widget::~Widget(){ delete ui; sensor->stop(); delete filter; delete sensor;}void Widget::btnPressed(){ if(isRunning) { sensor->stop(); ui->btnStart->setText("Start"); } else { sensor->start(); ui->btnStart->setText("Pause"); } isRunning = !isRunning;}
The code is relatively simple, but I have explained it more,The main sensor methods are Start and Stop. Use addfilter to connect the sensor with the filter.
6.
Finally, modify mysensor. Pro as follows (note that we have added mobility = sensors, which is very important ):
#-------------------------------------------------## Project created by QtCreator 2012-06-06T12:58:51##-------------------------------------------------QT += core guiTARGET = MySensorTEMPLATE = appSOURCES += main.cpp\ widget.cpp \ myfilter.cppHEADERS += widget.h \ myfilter.hFORMS += widget.uiCONFIG += mobilityMOBILITY = sensorssymbian { TARGET.UID3 = 0xe4448359 TARGET.EPOCSTACKSIZE = 0x14000 TARGET.EPOCHEAPSIZE = 0x020000 0x800000}
7.
Finally, set it to self-signed on the project tab.
8.
Connect to the mobile phone and use QT to download and run the following results:
Mobile phone:
Results uploaded to PC:
Finally, I uploaded the Code:
Http://download.csdn.net/detail/htttw/4355753
Done!