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)
方法
- void connectToEndpoint(endpointDescription)
- void disconnectFromEndpoint()
- bool readNodeAttributes(valuesToBeRead)
- bool 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 [read-only]
返回所有可用后端的名称列表。这些名称用于在连接时选择后端。
另请参阅 Connection::backend 。
backend : string
设置连接服务器时要使用的后端。必须在尝试建立任何连接之前进行设置。
另请参阅 Connection::availableBackends 。
connected : bool [read-only]
连接状态。若存在连接,则返回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 [read-only]
已连接服务器上注册的所有命名空间 URI 的字符串列表。
supportedSecurityPolicies : stringlist
包含受支持的安全策略的字符串列表
该属性目前处于技术预览阶段,因此所提供的 API 和功能可能会随时变更,恕不另行通知。
supportedUserTokenTypes : array[tokenTypes]
包含所有受支持的用户令牌类型的用户令牌策略类型数组。
该属性目前处于技术预览阶段,因此所提供的 API 和功能可能会随时变更,恕不另行通知。
Signal 文档
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 。
方法文档
void connectToEndpoint(endpointDescription)
连接到通过 `endpointDescription` 指定的端点。
另请参阅 EndpointDescription 。
void disconnectFromEndpoint()
断开已建立的连接。
bool 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 。
bool 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() 提供。
© 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.