Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Code Examples Sensors API/es: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 25: Line 25:
<code>#include <QAccelerometer>
<code>#include <QAccelerometer>


// Neccessary for Qt Mobility API usage<br />QTM_USE_NAMESPACE
// Neccessary for Qt Mobility API usage
QTM_USE_NAMESPACE


class AccelerationInfo : public QObject, public QAccelerometerFilter<br />{<br /> Q_OBJECT
class AccelerationInfo : public QObject, public QAccelerometerFilter
{
Q_OBJECT


public:
public:


AccelerationInfo(QObject* parent = 0) : QObject(parent)<br /> {<br /> m_sensor = new QAccelerometer(this);<br /> m_sensor->addFilter(this);<br /> m_sensor->start();<br /> }
AccelerationInfo(QObject* parent = 0) : QObject(parent)
{
m_sensor = new QAccelerometer(this);
m_sensor->addFilter(this);
m_sensor->start();
}


private slots:
private slots:


// Override of QAcclerometerFilter::filter(QAccelerometerReading*)<br /> void filter(QAccelerometerReading* reading)<br /> {<br /> qreal x = reading->x();<br /> qreal y = reading->y();<br /> qreal z = reading->z();
// Override of QAcclerometerFilter::filter(QAccelerometerReading*)
void filter(QAccelerometerReading* reading)
{
qreal x = reading->x();
qreal y = reading->y();
qreal z = reading->z();


// Process acceleration sensor readings …
// Process acceleration sensor readings …


qDebug("Current device acceleration: x=%f y=%f z=%f", x, y, z);<br /> }
qDebug("Current device acceleration: x=%f y=%f z=%f", x, y, z);
}


private:
private:


QAccelerometer* m_sensor;<br />};
QAccelerometer* m_sensor;
};


</code>
</code>
Line 53: Line 68:
Aquí está el código relevante del acelerómetro:
Aquí está el código relevante del acelerómetro:


<code>GLWidget::GLWidget(QWidget *parent) :<br /> QGLWidget(parent)
<code>GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)


{<br /> setWindowTitle(tr("Sensor-GL-Demo"));<br /> makeCurrent();
{
setWindowTitle(tr("Sensor-GL-Demo"));
makeCurrent();


setAttribute(Qt::WA_PaintOnScreen);<br /> setAttribute(Qt::WA_NoSystemBackground);<br /> setAutoBufferSwap(false);
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
setAutoBufferSwap(false);


xRot = 0;<br /> yRot = 0;<br /> zRot = 0;
xRot = 0;
yRot = 0;
zRot = 0;


_rotationSensorAvailable = false;<br /> _rotationSensor = new QtMobility::QAccelerometer(this);<br /> _rotationSensor->connect();<br /> if (!_rotationSensor->isAvailable()) {<br /> qWarning("Kein Beschleunigungssensor verfügbar!");<br /> } else {<br /> _rotationSensorAvailable = true;<br /> _rotationSensor->setSignalEnabled(false); // wir holen uns die Werte selbst ab<br /> _rotationSensor->setUpdateInterval(100); // so schnell wie möglich<br /> _rotationSensor->start();<br /> }
_rotationSensorAvailable = false;
_rotationSensor = new QtMobility::QAccelerometer(this);
_rotationSensor->connect();
if (!_rotationSensor->isAvailable()) {
qWarning("Kein Beschleunigungssensor verfügbar!");
} else {
_rotationSensorAvailable = true;
_rotationSensor->setSignalEnabled(false); // wir holen uns die Werte selbst ab
_rotationSensor->setUpdateInterval(100); // so schnell wie möglich
_rotationSensor->start();
}


QTimer *timer = new QTimer(this);<br /> timer->setInterval(10);<br /> QObject::connect(timer, SIGNAL (timeout()), this, SLOT (updateGL()));<br /> timer->start();<br /> showFullScreen();<br />}<br /></code>
QTimer *timer = new QTimer(this);
timer->setInterval(10);
QObject::connect(timer, SIGNAL (timeout()), this, SLOT (updateGL()));
timer->start();
showFullScreen();
}
</code>

Revision as of 11:53, 25 February 2015


[toc align_right="yes" depth="2"]

Spanish English

Información general

Esta página proporciona información general sobre ejemplos de código que utilizan la "Sensors API":http://doc.qt.nokia.com/qtmobility-1.1.0/sensors-api.html, una de las tantas APIs que forman parte de la suite Qt Mobilty.

Detector de caídas

El "ejemplo del Detecto de caídas":http://wiki.forum.nokia.com/index.php/Qt_Mobility_example_application:_Fall_Detector de Forum Nokia usa el acelerómetro en un dispositivo móvil para detectar si un usuario se ha caído, este ejemplo utiliza:

Fall Dector Screen Shot

Aquí esta el fragmento de código del detector de caídas usando el API de Sensores:

#include <QAccelerometer>

// Neccessary for Qt Mobility API usage
QTM_USE_NAMESPACE

class AccelerationInfo : public QObject, public QAccelerometerFilter
{
 Q_OBJECT

public:

AccelerationInfo(QObject* parent = 0) : QObject(parent)
 {
 m_sensor = new QAccelerometer(this);
 m_sensor->addFilter(this);
 m_sensor->start();
 }

private slots:

// Override of QAcclerometerFilter::filter(QAccelerometerReading*)
 void filter(QAccelerometerReading* reading)
 {
 qreal x = reading->x();
 qreal y = reading->y();
 qreal z = reading->z();

// Process acceleration sensor readings …

qDebug("Current device acceleration: x=%f y=%f z=%f", x, y, z);
 }

private:

QAccelerometer* m_sensor;
};

Usando el acelerómetro para controlar un modelo Open GL-ES 3D

El tutorial sobre sensores en "Mobile Qt-Entwicklung":http://www.mobileqt.de/wiki/daten_des_accelerometer_sensors_mit_einem_opengl_objekt_verknuepfen (Mobile Qt Development) muestra el acelerometro en un N900 manipulando un modelo 3D que ha sido creado usando Open GL. El "texto descriptivo esta en Alemán":http://www.mobileqt.de/wiki/daten_des_accelerometer_sensors_mit_einem_opengl_objekt_verknuepfen, pero todo "el codigo esta disponible para descargar en un paquete":http://www.mobileqt.de/tutorials/6/glsensordemo-0.1.zip y esta "demo en vídeo":http://www.youtube.com/watch?v=uJpw0yeHJl8 muestra como funciona.

Aquí está el código relevante del acelerómetro:

GLWidget::GLWidget(QWidget *parent) :
 QGLWidget(parent)

{
 setWindowTitle(tr("Sensor-GL-Demo"));
 makeCurrent();

setAttribute(Qt::WA_PaintOnScreen);
 setAttribute(Qt::WA_NoSystemBackground);
 setAutoBufferSwap(false);

xRot = 0;
 yRot = 0;
 zRot = 0;

_rotationSensorAvailable = false;
 _rotationSensor = new QtMobility::QAccelerometer(this);
 _rotationSensor->connect();
 if (!_rotationSensor->isAvailable()) {
 qWarning("Kein Beschleunigungssensor verfügbar!");
 } else {
 _rotationSensorAvailable = true;
 _rotationSensor->setSignalEnabled(false); // wir holen uns die Werte selbst ab
 _rotationSensor->setUpdateInterval(100); // so schnell wie möglich
 _rotationSensor->start();
 }

QTimer *timer = new QTimer(this);
 timer->setInterval(10);
 QObject::connect(timer, SIGNAL (timeout()), this, SLOT (updateGL()));
 timer->start();
 showFullScreen();
}