Connection QML Type | Qt OPC UA 6.4.1

- Qt 6.4
- Qt OPC UA
- QML Types
- Connection
- Qt 6.4.1 Reference Documentation

# Connection QML Type

Connects to a server. More...

Import Statement: | import QtOpcUa
Since: | QtOpcUa 5.12

- List of all members, including inherited members

## Properties

- authenticationInformation : AuthenticationInformation
- availableBackends : stringlist
- backend : string
- connected : bool
- connection : QOpcUaClient
- currentEndpoint : QOpcUaEndpointDescription
- defaultConnection : bool
- namespaces : stringlist
- supportedSecurityPolicies : stringlist
- supportedUserTokenTypes : array[tokenTypes]

## Signals

- nodeChanged()
- readNodeAttributesFinished(readResults)
- writeNodeAttributesFinished(writeResults)

## Methods

- connectToEndpoint(endpointDescription)
- disconnectFromEndpoint()
- readNodeAttributes(valuesToBeRead)
- writeNodeAttributes(valuesToBeWritten)

## Detailed Description

The main API uses backends to make connections. You have to set the backend before any connection attempt.

     import QtOpcUa as QtOpcUa

     QtOpcUa.Connection {
         backend: "open62541"
     }

     Component.onCompleted: {
         connection.connectToEndpoint("opc.tcp://127.0.0.1:43344");
     }

## Property Documentation

authenticationInformation : AuthenticationInformation

Set the authentication information to this connection. The authentication information has to be set before calling connectToEndpoint. If no authentication information is set, the anonymous mode will be used. It has no effect on the current connection. If the client is disconnected and then reconnected, the new credentials are used. Reading and writing this property before a backend is set, writes are ignored and reads return and invalid AuthenticationInformation.

[read-only] availableBackends : stringlist

Returns the names of all available backends as a list. These are used to select a backend when connecting.

See also Connection::backend.

backend : string

Set the backend to use for a connection to the server. Has to be set before any connection attempt.

See also Connection::availableBackends.

[read-only] connected : bool

Status of the connection. `true` when there is a connection, otherwise `false`.

[since 5.13] connection : QOpcUaClient

This property is used only to inject a connection from C++. In case of complex setup of a connection you can use C++ to handle all the details. After the connection is established it can be handed to QML using this property. Ownership of the client is transferred to 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 *);

Emitting the signal `connectionChanged` when the client setup is completed, the QML code below will use the connection.

     import QtOpcUa as QtOpcUa

     MyClass {
         id: myclass
     }

     QtOpcUa.Connection {
         connection: myclass.connection
      }

This property was introduced in Qt 5.13.

[since 5.13] currentEndpoint : QOpcUaEndpointDescription

An endpoint description of the server to which the connection is connected to. When the connection is not established, an empty endpoint description is returned.

This property was introduced in Qt 5.13.

defaultConnection : bool

Makes this the default connection. Usually each node needs to be given a connection to use. If this property is set to `true`, this connection will be used in all cases where a node has no connection set. Already established connections are not affected. If `defaultConnection` is set to `true` on multiple connection the last one is used.

     QtOpcUa.Connection {
         ...
         defaultConnection: true
         ...
     }

See also Node.

[read-only] namespaces : stringlist

List of strings of all namespace URIs registered on the connected server.

[since 5.13] supportedSecurityPolicies : stringlist

A list of strings containing the supported security policies

This property is currently available as a Technology Preview, and therefore the API and functionality provided may be subject to change at any time without prior notice.

This property was introduced in Qt 5.13.

[since 5.13] supportedUserTokenTypes : array[tokenTypes]

An array of user token policy types of all supported user token types.

This property is currently available as a Technology Preview, and therefore the API and functionality provided may be subject to change at any time without prior notice.

This property was introduced in Qt 5.13.

## Signal Documentation

nodeChanged()

Emitted when the underlying node has changed. This happens when the namespace or identifier of the NodeId changed.

Note: The corresponding handler is `onNodeChanged`.

`[since 5.13]` readNodeAttributesFinished(readResults)

Emitted when the read request, started using readNodeAttributes(), is finished. The readResults parameter is an array of ReadResult entries, containing the values requested from the server.

     connection.onReadNodeAttributesFinished(results) {
         for (var i = 0; results.length; i++) {
             if (results[i].status.isGood) {
                 console.log(results[i].value);
             } else {
                 // handle error
             }
         }
     }

Note: The corresponding handler is `onReadNodeAttributesFinished`.

This signal was introduced in Qt 5.13.

See also readNodeAttributes() and ReadResult.

`[since 5.13]` writeNodeAttributesFinished(writeResults)

Emitted when the write request started using writeNodeAttributes() is finished. The writeResults parameter is an array of WriteResult entries, containing the values requested from the server.

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

Note: The corresponding handler is `onWriteNodeAttributesFinished`.

This signal was introduced in Qt 5.13.

See also writeNodeAttributes() and WriteResult.

## Method Documentation

connectToEndpoint(endpointDescription)

Connects to the endpoint specified with endpointDescription.

See also EndpointDescription.

disconnectFromEndpoint()

Disconnects an established connection.

readNodeAttributes(valuesToBeRead)

This function is used to read multiple values from a server in one go. Returns `true` if the read request was dispatched successfully.

The valuesToBeRead parameter must be a JavaScript array of ReadItem entries.

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

The result of the read request are provided by the signal readNodeAttributesFinished().

See also readNodeAttributesFinished() and ReadItem.

writeNodeAttributes(valuesToBeWritten)

This function is used to write multiple values to a server in one go. Returns `true` if the write request was dispatched successfully.

The valuesToBeWritten parameter must be a JavaScript array of WriteItem entries.

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

The result of the write request are provided by the signal Connection::writeNodeAttributesFinished().

See also Connection::writeNodeAttributesFinished() and WriteItem.