センサープラグインの作成

センサープラグインのロード方法

センサーのバックエンドはオンデマンドで作成されるため、センサープラグインがロードされ、扱うセンサーのバックエンドを登録するように求められます。プラグインはQSensorPluginInterface::registerSensors() を実装し、利用可能なバックエンドを登録するためにQSensorManager::registerBackend() を呼び出す必要があります。通常、プラグインはQSensorBackendFactory を継承し、登録したバックエンドをインスタンス化するためにQSensorBackendFactory::createBackend() を実装します。

最も単純なプラグインはセンサーバックエンドを1つだけ持ちますが、複数のセンサーバックエンドをプラグインに持つことができない理由はありません。

以下に例を示す。

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;
    }
};

© 2025 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.