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 属性。
对于数据类型定义属性中存在QOpcUaStructureDefinition 值的结构化类型,可以对包含它们的扩展对象进行自动解码。具有内置类型或存在 C++ 数据类的字段会被反序列化为相应的Qt OPC UA 类型,其他通用结构体会被序列化为嵌套的QOpcUaGenericStructValue 。所有嵌套的通用结构值都必须在服务器中具有QOpcUaStructureDefinition ,否则解码会失败。
同样的条件也适用于自定义结构体的编码。
自定义结构解码示例
QOpcUaGenericStructHandlerhandler(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(节点, &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::Unknown | 0 | 类型节点 id 未知。 |
QOpcUaGenericStructHandler::DataTypeKind::Struct | 1 | 类型节点 id 属于结构化类型。 |
QOpcUaGenericStructHandler::DataTypeKind::Enum | 2 | 类型节点 id 属于枚举类型。 |
QOpcUaGenericStructHandler::DataTypeKind::Other | 3 | 类型节点 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
返回类型节点 idid 的数据类型种类。
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
返回类型节点 idid 的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
返回二进制编码节点 idid 的QOpcUaStructureDefinition 。如果节点 id 未知或不属于结构类型,则返回默认构造值。
QOpcUaStructureDefinition QOpcUaGenericStructHandler::structureDefinitionForTypeId(const QString &id) const
返回类型节点 idid 的QOpcUaStructureDefinition 。如果节点 id 未知或不属于结构类型,则返回默认构造值。
QString QOpcUaGenericStructHandler::typeIdForBinaryEncodingId(const QString &id) const
返回与二进制编码 ID 相关联的类型节点 IDid 。
QString QOpcUaGenericStructHandler::typeNameForBinaryEncodingId(const QString &id) const
返回属于二进制编码节点 idid 的类型名称。如果节点 id 未知或不属于结构类型,则返回空字符串。
QString QOpcUaGenericStructHandler::typeNameForTypeId(const QString &id) const
返回属于由类型节点 idid 标识的数据类型节点的类型名称。如果节点 id 不详,则返回空字符串。
© 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.