summaryrefslogtreecommitdiffstats
path: root/src/sensors/doc/snippets/sensors/start.cpp
blob: 9b52c1ea62f8dda672c520558957af2444f02abb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QtSensors/qsensor.h>
#include <QtCore/QMetaObject>
#include <QtCore/QMetaproperty>

void start()
{
//! [Starting a sensor]
// start the sensor
QSensor sensor("QAccelerometer");
sensor.start();

// later
QSensorReading *reading = sensor.reading();
qreal x = reading->property("x").value<qreal>();
qreal y = reading->value(1).value<qreal>();
//! [Starting a sensor]

    Q_UNUSED(x);
    Q_UNUSED(y);
}

class MyObject : public QObject
{
    void findSensors()
    {
        //! [Find sensors]
        QList<QSensor*> mySensorList;
        for (const QByteArray &type : QSensor::sensorTypes()) {
            qDebug() << "Found a sensor type:" << type;
            for (const QByteArray &identifier : QSensor::sensorsForType(type)) {
                qDebug() << "    " << "Found a sensor of that type:" << identifier;
                QSensor* sensor = new QSensor(type, this);
                sensor->setIdentifier(identifier);
                mySensorList.append(sensor);
            }
        }
        //! [Find sensors]
        //! [Print reading properties]
        for (QSensor* sensor : mySensorList) {
            const int firstProperty = QSensorReading::staticMetaObject.propertyOffset();
            // Connect to backend first in case start() hasn't been called yet
            if (!sensor->connectToBackend())
                continue;
            qDebug() << "Sensor" << sensor->identifier() << "reading properties:";
            QSensorReading *reading = sensor->reading();
            if (reading) {
                const QMetaObject *mo = reading->metaObject();
                for (int i = firstProperty; i < mo->propertyCount(); ++i) {
                    QByteArray name = mo->property(i).name();
                    qDebug() << "    " << name << reading->property(name).toByteArray();
                }
            }
        }
        //! [Print reading properties]
    }
};