Connection QML Type
连接服务器。更多
Import Statement: | import QtOpcUa |
Since: | QtOpcUa 5.12 |
Status: | Deprecated since 6.9 |
自 QtOpcUa 6.9 起,该类型已被弃用。我们强烈建议不要在新代码中使用它。
属性
- authenticationInformation : AuthenticationInformation
- availableBackends : stringlist
- backend : string
- connected : bool
- connection : QOpcUaClient
- currentEndpoint : QOpcUaEndpointDescription
- defaultConnection : bool
- namespaces : stringlist
- supportedSecurityPolicies : stringlist
- supportedUserTokenTypes : array[tokenTypes]
信号
- nodeChanged()
- readNodeAttributesFinished(readResults)
- writeNodeAttributesFinished(writeResults)
方法
- connectToEndpoint(endpointDescription)
- disconnectFromEndpoint()
- readNodeAttributes(valuesToBeRead)
- writeNodeAttributes(valuesToBeWritten)
详细说明
主 API 使用后端进行连接。您必须在尝试任何连接之前设置后端。
import QtOpcUa as QtOpcUa QtOpcUa.Connection { backend: "open62541" } Component.onCompleted: { connection.connectToEndpoint("opc.tcp://127.0.0.1:43344"); }
属性文档
authenticationInformation : AuthenticationInformation |
为该连接设置身份验证信息。必须在调用connectToEndpoint 之前设置身份验证信息。如果未设置验证信息,则将使用匿名模式。这对当前连接没有影响。如果客户端断开连接,然后重新连接,则会使用新的凭据。在backend 设置之前读写此属性,写入将被忽略,读取将返回无效的AuthenticationInformation 。
availableBackends : stringlist |
以列表形式返回所有可用后端的名称。这些名称用于在连接时选择后端。
另请参阅 Connection::backend 。
backend : string |
设置用于连接服务器的后端。必须在尝试任何连接之前设置。
connected : bool |
连接状态。有连接时为true
,否则为false
。
connection : QOpcUaClient |
该属性仅用于从 C++ 注入连接。如果连接设置复杂,可以使用 C++ 处理所有细节。连接建立后,可使用此属性将其交给 QML。客户端的所有权将转移给 QML。
class MyClass : public QObject { Q_OBJECT Q_PROPERTY(QOpcUaClient* connection READ connection NOTIFY connectionChanged) public: MyClass (QObject* parent = nullptr); QOpcUaClient *connection() const; signals: void connectionChanged(QOpcUaClient *);
当客户端设置完成时,发射信号connectionChanged
,下面的 QML 代码将使用该连接。
import QtOpcUa as QtOpcUa MyClass { id: myclass } QtOpcUa.Connection { connection: myclass.connection }
currentEndpoint : QOpcUaEndpointDescription |
连接所指向服务器的端点描述。当连接未建立时,将返回空的端点描述。
defaultConnection : bool |
使其成为默认连接。通常每个节点都需要使用一个连接。如果此属性设置为true
,则在节点未设置连接的所有情况下都将使用此连接。已建立的连接不受影响。如果defaultConnection
在多个连接上被设置为true
,则使用最后一个连接。
QtOpcUa.Connection { ... defaultConnection: true ... }
另请参阅 Node 。
namespaces : stringlist |
连接服务器上注册的所有命名空间 URI 的字符串列表。
supportedSecurityPolicies : stringlist |
包含支持的安全策略的字符串列表
此属性目前为技术预览版,因此所提供的 API 和功能可能会随时更改,恕不另行通知。
supportedUserTokenTypes : array[tokenTypes] |
所有支持的用户令牌类型的用户令牌策略类型数组。
该属性目前为技术预览版,因此所提供的 API 和功能可能会随时更改,恕不另行通知。
信号文档
nodeChanged() |
当底层节点发生变化时发出。这种情况发生在NodeId 的命名空间或标识符发生变化时。
注: 相应的处理程序是onNodeChanged
。
readNodeAttributesFinished(readResults) |
当使用readNodeAttributes() 启动的读取请求完成时发出。readResults 参数是ReadResult 条目的数组,包含从服务器请求的值。
connection.onReadNodeAttributesFinished(results) { for (var i = 0; results.length; i++) { if (results[i].status.isGood) { console.log(results[i].value); } else { // handle error } } }
注: 相应的处理程序是onReadNodeAttributesFinished
。
另请参阅 readNodeAttributes() 和ReadResult 。
writeNodeAttributesFinished(writeResults) |
当使用writeNodeAttributes() 启动的写入请求完成时发出。writeResults 参数是WriteResult 条目的数组,包含从服务器请求的值。
for (var i = 0; i < writeResults.length; i++) { console.log(writeResults[i].nodeId); console.log(writeResults[i].namespaceName); console.log(writeResults[i].attribute); if (writeResults[i].status.isBad) { // value was not written } }
注: 相应的处理程序是onWriteNodeAttributesFinished
。
另请参阅 writeNodeAttributes() 和WriteResult 。
方法文档
connectToEndpoint(endpointDescription) |
连接到endpointDescription 指定的端点。
另请参阅 EndpointDescription 。
disconnectFromEndpoint() |
断开已建立的连接。
readNodeAttributes(valuesToBeRead) |
该函数用于一次性从服务器读取多个值。如果读取请求发送成功,则返回true
。
valuesToBeRead 参数必须是由ReadItem 条目组成的 JavaScript 数组。
// List of items to read var readItemList = []; // Item to be added to the list of items to be read var readItem; // Prepare an item to be read // Create a new read item and fill properties readItem = QtOpcUa.ReadItem.create(); readItem.ns = "http://qt-project.org"; readItem.nodeId = "s=Demo.Static.Scalar.Double"; readItem.attribute = QtOpcUa.Constants.NodeAttribute.DisplayName; // Add the prepared item to the list of items to be read readItemList.push(readItem); // Add further items [...] if (!connection.readNodeAttributes(readItemList)) { // handle error }
读取请求的结果由信号readNodeAttributesFinished() 提供。
另请参阅 readNodeAttributesFinished() 和ReadItem 。
writeNodeAttributes(valuesToBeWritten) |
该函数用于一次性向服务器写入多个值。如果写入请求发送成功,则返回true
。
valuesToBeWritten 参数必须是由WriteItem 条目组成的 JavaScript 数组。
// List of items to write var writeItemList = []; // Item to be added to the list of items to be written var writeItem; // Prepare an item to be written // Create a new write item and fill properties writeItem = QtOpcUa.WriteItem.create(); writeItem.ns = "http://qt-project.org"; writeItem.nodeId = "s=Demo.Static.Scalar.Double"; writeItem.attribute = QtOpcUa.Constants.NodeAttribute.Value; writeItem.value = 32.1; writeItem.valueType = QtOpcUa.Constants.Double; // Add the prepared item to the list of items to be written writeItemList.push(writeItem); // Add further items [...] if (!connection.writeNodeAttributes(writeItemList)) { // handle error }
写入请求的结果由信号Connection::writeNodeAttributesFinished() 提供。
© 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.