In questa pagina

Creazione di un plugin per sensori

Come viene caricato un plugin per sensori

Poiché i backend dei sensori vengono creati su richiesta, il plugin del sensore viene caricato e gli viene richiesto di registrare i backend dei sensori che gestisce. Il plugin dovrebbe implementare QSensorPluginInterface::registerSensors() e chiamare QSensorManager::registerBackend() per registrare i backend disponibili. In genere il plugin erediterà anche da QSensorBackendFactory e implementerà QSensorBackendFactory::createBackend() per istanziare i backend che ha registrato.

Il plugin più semplice avrà un solo backend per sensori, sebbene non vi sia alcun motivo per cui un plugin non possa contenere più backend per sensori.

Di seguito è riportato un esempio.

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.