diff options
Diffstat (limited to 'src/sensors/doc/snippets/sensors/start.cpp')
-rw-r--r-- | src/sensors/doc/snippets/sensors/start.cpp | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/sensors/doc/snippets/sensors/start.cpp b/src/sensors/doc/snippets/sensors/start.cpp index 2ff9c89c..18c87d06 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 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#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] + } +}; |