diff options
author | Juha Vuolle <juha.vuolle@qt.io> | 2023-02-17 17:18:44 +0200 |
---|---|---|
committer | Juha Vuolle <juha.vuolle@qt.io> | 2023-02-22 08:38:04 +0200 |
commit | 039ebce49c1f6aaf5ffc79dcfeb924dd8b24e8a0 (patch) | |
tree | 15a587468a6bbcb43d29e5705307c8d82af65c85 | |
parent | 99e44c23abf5d87fbd16a87bd058c776179e8343 (diff) | |
download | qtsensors-039ebce49c1f6aaf5ffc79dcfeb924dd8b24e8a0.tar.gz |
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
Pick-to: 6.5
Change-Id: Id5dceb8bdb5833ebb69818146d163200a93a52e0
Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
-rw-r--r-- | src/sensors/doc/snippets/sensors/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/sensors/doc/snippets/sensors/start.cpp | 40 | ||||
-rw-r--r-- | src/sensors/doc/src/qtsensors-cpp.qdoc | 14 |
3 files changed, 52 insertions, 3 deletions
diff --git a/src/sensors/doc/snippets/sensors/CMakeLists.txt b/src/sensors/doc/snippets/sensors/CMakeLists.txt index 16c4bea..236ffce 100644 --- a/src/sensors/doc/snippets/sensors/CMakeLists.txt +++ b/src/sensors/doc/snippets/sensors/CMakeLists.txt @@ -17,6 +17,7 @@ qt_add_executable(sensorsdocsnippet main.cpp mybackend.h plugin.cpp + start.cpp ) target_link_libraries(sensorsdocsnippet PUBLIC diff --git a/src/sensors/doc/snippets/sensors/start.cpp b/src/sensors/doc/snippets/sensors/start.cpp index 2ff9c89..9b52c1e 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] + } +}; diff --git a/src/sensors/doc/src/qtsensors-cpp.qdoc b/src/sensors/doc/src/qtsensors-cpp.qdoc index 2251289..ce512bf 100644 --- a/src/sensors/doc/src/qtsensors-cpp.qdoc +++ b/src/sensors/doc/src/qtsensors-cpp.qdoc @@ -79,8 +79,18 @@ This code does not require any compile-time links to \l QAccelerometer or \snippet sensors/start.cpp Starting a sensor -You can discover all of this information at runtime too. The sensor_explorer example -shows you information about available sensors. +You can discover all of this information at runtime too. + +\section1 Discovering Sensors And Reading Properties At Runtime + +Sometimes it may be that the available sensors are not known at development time. +It is possible to find out which sensors are available as illustrated below: + +\snippet sensors/start.cpp Find sensors + +Furthermore it is possible to discover the reading details for these sensors, as illustrated below: + +\snippet sensors/start.cpp Print reading properties \section1 Front End, Back End |