Nesta página

Criação de um plug-in de sensor

Como um plug-in de sensor é carregado

Como os backends de sensores são criados sob demanda, o plug-in de sensor é carregado e solicitado a registrar os backends de sensores que ele suporta. O plug-in deve implementar ` QSensorPluginInterface::registerSensors()` e chamar ` QSensorManager::registerBackend()` para registrar os backends disponíveis. Normalmente, o plug-in também herdará de ` QSensorBackendFactory ` e implementará ` QSensorBackendFactory::createBackend()` para instanciar os backends que registrou.

O plug-in mais simples terá apenas um backend de sensor, embora não haja motivo para que um plug-in não possa conter vários backends de sensor.

Segue um exemplo.

class MyPluginClass : public QObject, public QSensorPluginInterface, public QSensorBackendFactory
{
    Q_OBJECT
    //Q_PLUGIN_METADATA(IID "com.qt-project.Qt.QSensorPluginInterface/1.0" FILE "plugin.json")
    Q_INTERFACES(QSensorPluginInterface)
public:
    void registerSensors() override
    {
        QSensorManager::registerBackend(QAccelerometer::sensorType, MyBackend::id, this);
    }

    QSensorBackend *createBackend(QSensor *sensor) override
    {
        if (sensor->identifier() == MyBackend::id)
            return new MyBackend(sensor);
        return 0;
    }
};

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.