このページでは

QOpcUaGenericStructHandler Class

サーバーのデータ型を読み取り、汎用構造体を拡張オブジェクトからデコードしたり、拡張オブジェクトへエンコードしたりします。詳細...

ヘッダー: #include <QOpcUaGenericStructHandler>
CMake: find_package(Qt6 REQUIRED COMPONENTS OpcUa)
target_link_libraries(mytarget PRIVATE Qt6::OpcUa)
qmake: QT += opcua
以下のように: Qt 6.7 以降
継承元: QObject

パブリック型

enum class DataTypeKind { Unknown, Struct, Enum, Other }

パブリック関数

QOpcUaGenericStructHandler(QOpcUaClient *client, QObject *parent = nullptr)
bool addCustomEnumDefinition(const QOpcUaEnumDefinition &definition, const QString &typeId, const QString &name, QOpcUa::IsAbstract isAbstract = QOpcUa::IsAbstract::NotAbstract)
bool addCustomStructureDefinition(const QOpcUaStructureDefinition &definition, const QString &typeId, const QString &name, QOpcUa::IsAbstract isAbstract = QOpcUa::IsAbstract::NotAbstract)
QOpcUaGenericStructValue createGenericStructValueForTypeId(const QString &typeId)
QOpcUaGenericStructHandler::DataTypeKind dataTypeKindForTypeId(const QString &id) const
std::optional<QOpcUaGenericStructValue> decode(const QOpcUaExtensionObject &extensionObject) const
std::optional<QOpcUaExtensionObject> encode(const QOpcUaGenericStructValue &value)
QOpcUaEnumDefinition enumDefinitionForTypeId(const QString &id) const
bool initialize()
(since 6.7) bool initialized() const
bool isAbstractTypeId(const QString &id) const
QOpcUaStructureDefinition structureDefinitionForBinaryEncodingId(const QString &id) const
QOpcUaStructureDefinition structureDefinitionForTypeId(const QString &id) const
QString typeIdForBinaryEncodingId(const QString &id) const
QString typeNameForBinaryEncodingId(const QString &id) const
QString typeNameForTypeId(const QString &id) const

シグナル

void initializedChanged(bool initialized)

詳細な説明

OPC UAで使用されるバイナリデータエンコーディングは、メッセージサイズを小さくすることを念頭に設計されており、データの構造に関する情報は一切含まれていません。つまり、デコーダがデータバッファをデコードするには、エンコードされたデータの構造を事前に把握している必要があります。

OPC UA 1.04以降、DataTypeノードクラスのノードには、構造化型のフィールドに関する情報や、列挙値と名前とのマッピング情報を含むDataTypeDefinition属性を設定できるようになりました。これにより、組み込み型のデコード方法に関する知識と組み合わせることで、クライアントは外部の知識に依存することなく、汎用的なカスタム構造化型をデコードできるようになります。

QOpcUaGenericStructHandlerは、BaseDataTypeを起点としてHasSubtype参照をたどることでサーバーの型階層を走査し、ノードのDataTypeDefinition属性を読み取ります。

DataTypeDefinition属性にQOpcUaStructureDefinition 値が存在する構造化型については、それらを含む拡張オブジェクトの自動デコードが可能です。 組み込み型または C++ データクラスが存在する型を持つフィールドは、対応する `Qt OPC UA ` 型に逆シリアル化され、その他のジェネリック構造体はネストされた `QOpcUaGenericStructValue` にシリアル化されます。すべてのネストされたジェネリック構造体の値は、サーバー内に `QOpcUaStructureDefinition ` を持つ必要があり、そうでない場合、デコードは失敗します。

カスタム構造体のエンコードについても、同様の条件が適用されます。

カスタム構造体のデコード例:

QOpcUaGenericStructHandler handler(opcuaClient);
handler.initialize();

QObject::connect(&handler, &QOpcUaGenericStructHandler::initializedChanged, [opcuaClient, &handler](boolinitialized) {
    if(!initialized)
        return;

   autonode= opcuaClient->node("ns=4;i=3003");// open62541-testserver 内のカスタム構造体テストノード
   node->readValueAttribute();

    QObject::connect(node, &QOpcUaNode::attributeRead, [node, &handler](QOpcUa::NodeAttributes attr) {
       if(!attr.testFlag(QOpcUa::NodeAttribute::Value)|| node->valueAttributeError()!= QOpcUa::UaStatusCode::Good)
            return;

        autoextObj= node->valueAttribute().value<QOpcUaExtensionObject>();
        qDebug() << "Got object of type" << handler.typeNameForBinaryEncodingId(extObj.encodingTypeId());

       const autoresult=handler.decode(extObj);

        if(!result)
            return;

        qDebug() << *result;
    });
});

カスタム構造体をエンコードする例:

QOpcUaGenericStructHandler handler(opcuaClient);
handler.initialize();

QObject::connect(&handler, &QOpcUaGenericStructHandler::initializedChanged, [opcuaClient, &handler](bool initialized) {
    if (!initialized)
        return;

    QOpcUaGenericStructValue value = handler.createGenericStructValueForTypeId("ns=4;i=3006");
    value.fieldsRef()["MandatoryMember"] = 23.0;
    value.fieldsRef()["OptionalMember"] = 42.0;

    const auto ext = handler.encode(value);

    if (!ext)
        return;

    // Use the extension object to write a node's value attribute, in a method parameter, etc...
});

メンバ型のドキュメント

enum class QOpcUaGenericStructHandler::DataTypeKind

この列挙型は、データ型ノードのデータ型の種類を指定します。

定数値説明
QOpcUaGenericStructHandler::DataTypeKind::Unknown0型ノード ID は不明です。
QOpcUaGenericStructHandler::DataTypeKind::Struct1型ノード ID は構造化型に属しています。
QOpcUaGenericStructHandler::DataTypeKind::Enum2その型ノード ID は enum 型に属しています。
QOpcUaGenericStructHandler::DataTypeKind::Other3この型ノード ID は、構造体や列挙型ではない型(その他の組み込み型またはそのサブタイプ)に属しています。

メンバ関数のドキュメント

[explicit] QOpcUaGenericStructHandler::QOpcUaGenericStructHandler(QOpcUaClient *client, QObject *parent = nullptr)

client 用の汎用構造体ハンドラを構築します。

bool QOpcUaGenericStructHandler::addCustomEnumDefinition(const QOpcUaEnumDefinition &definition, const QString &typeId, const QString &name, QOpcUa::IsAbstract isAbstract = QOpcUa::IsAbstract::NotAbstract)

カスタム列挙型定義 `definition ` を既知の型に追加します。これは、サーバーが `StructureDefinition` を公開していないカスタム構造体をサポートするために使用できます。適切なデコードおよびエンコードを行うには、パラメータ `definition`、`typeId `、および `name ` が必須です。`isAbstract ` が設定されている場合、その型はエンコードおよびデコードできません。

列挙型の定義が正常に追加された場合、true を返します。

bool QOpcUaGenericStructHandler::addCustomStructureDefinition(const QOpcUaStructureDefinition &definition, const QString &typeId, const QString &name, QOpcUa::IsAbstract isAbstract = QOpcUa::IsAbstract::NotAbstract)

カスタム構造体定義 `definition ` を既知の型に追加します。これは、サーバーが `StructureDefinition` を公開していないカスタム構造体をサポートするために使用できます。適切なデコードおよびエンコードを行うには、パラメータ `definition`、`typeId `、および `name ` が必須です。`isAbstract ` が設定されている場合、その型はエンコードおよびデコードできません。

構造体定義の追加に成功した場合は、true を返します。

QOpcUaGenericStructValue QOpcUaGenericStructHandler::createGenericStructValueForTypeId(const QString &typeId)

typeId に対応する構造体の定義、型 ID、および型名が事前に入力された汎用構造体の値を返します。すべての必須フィールドには、無効なプレースホルダー「QVariant 」が挿入されます。

QOpcUaGenericStructHandler::DataTypeKind QOpcUaGenericStructHandler::dataTypeKindForTypeId(const QString &id) const

型ノード ID「id 」のデータ型「kind」を返します。

std::optional<QOpcUaGenericStructValue> QOpcUaGenericStructHandler::decode(const QOpcUaExtensionObject &extensionObject) const

extensionObject をQOpcUaGenericStructValue にデコードします。デコーダーが失敗した場合は、std::nullopt が返されます。

std::optional<QOpcUaExtensionObject> QOpcUaGenericStructHandler::encode(const QOpcUaGenericStructValue &value)

QOpcUaExtensionObject としてエンコードされたvalue を返します。値をエンコードできなかった場合は、std::nullopt を返します。

QOpcUaEnumDefinition QOpcUaGenericStructHandler::enumDefinitionForTypeId(const QString &id) const

型ノード ID `id` に対応する `QOpcUaEnumDefinition ` を返します。ノード ID が不明であるか、列挙型に属していない場合は、デフォルトの構築値が返されます。

bool QOpcUaGenericStructHandler::initialize()

データ型階層の探索を開始します。成功または失敗は、initializedChanged シグナルで通知されます。

操作を開始できない場合は、false を返します。

[since 6.7] bool QOpcUaGenericStructHandler::initialized() const

汎用構造体ハンドラが初期化されている場合、true を返します。

この関数は Qt 6.7 で導入されました。

[signal] void QOpcUaGenericStructHandler::initializedChanged(bool initialized)

このシグナルは、初期化処理が完了したときに発信されます。initialized は、初期化が成功したかどうかを示します。

bool QOpcUaGenericStructHandler::isAbstractTypeId(const QString &id) const

id で指定されたデータ型が抽象型である場合、true を返します。

QOpcUaStructureDefinition QOpcUaGenericStructHandler::structureDefinitionForBinaryEncodingId(const QString &id) const

バイナリエンコーディングノード ID `id` に対応する `QOpcUaStructureDefinition ` を返します。ノード ID が不明であるか、構造体型に属していない場合は、デフォルト構築された値が返されます。

QOpcUaStructureDefinition QOpcUaGenericStructHandler::structureDefinitionForTypeId(const QString &id) const

型ノード ID `id` に対応する `QOpcUaStructureDefinition ` を返します。ノード ID が不明であるか、構造体型に属していない場合は、デフォルト構築された値が返されます。

QString QOpcUaGenericStructHandler::typeIdForBinaryEncodingId(const QString &id) const

バイナリエンコーディング ID「id 」に関連付けられたタイプノード ID を返します。

QString QOpcUaGenericStructHandler::typeNameForBinaryEncodingId(const QString &id) const

バイナリエンコーディングノード ID `id` に属する型名を返します。ノード ID が不明であるか、構造体型に属していない場合は、空文字列が返されます。

QString QOpcUaGenericStructHandler::typeNameForTypeId(const QString &id) const

型ノード ID(id )で識別されるデータ型ノードに属する型名を返します。ノード ID が不明な場合は、空の文字列が返されます。

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