diff options
author | Juha Vuolle <juha.vuolle@qt.io> | 2023-02-17 17:18:44 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-02-24 07:02:26 +0000 |
commit | 4debf462a0a5e8af5850e18b1fa755a413fdb87a (patch) | |
tree | 05f425b166a611f1bfabdb07ee38075cc87acf9e /src/sensors/doc/snippets/sensors/start.cpp | |
parent | adc9b97df60640e18650802a62e03856f0064ce8 (diff) |
Add iterating sensors and reading properties as doc snippet
This was previously illustrated by the sensor_explorer example, which
is now a manual test.
Task-number: QTBUG-110939
Change-Id: Id5dceb8bdb5833ebb69818146d163200a93a52e0
Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
(cherry picked from commit 039ebce49c1f6aaf5ffc79dcfeb924dd8b24e8a0)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/sensors/doc/snippets/sensors/start.cpp')
-rw-r--r-- | src/sensors/doc/snippets/sensors/start.cpp | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/sensors/doc/snippets/sensors/start.cpp b/src/sensors/doc/snippets/sensors/start.cpp index 2ff9c89c..9b52c1ea 100644 --- a/src/sensors/doc/snippets/sensors/start.cpp +++ b/src/sensors/doc/snippets/sensors/start.cpp @@ -1,7 +1,9 @@ // 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 <qsensor.h> +#include <QtSensors/qsensor.h> +#include <QtCore/QMetaObject> +#include <QtCore/QMetaproperty> void start() { @@ -19,3 +21,39 @@ qreal y = reading->value(1).value<qreal>(); 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] + } +}; |