From 47227acd25c59a1d4b2961c0e1b1eb879e68adec Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Fri, 20 Feb 2026 12:59:07 +0100 Subject: Add write support with automatic type coercion to OpcUaMonitoredNode writeValue() Q_INVOKABLE coerces QML JS types to the exact C++ type expected by the OPC UA node (auto-detected from DataType attribute via opcUaDataTypeToQOpcUaType). Handles all scalar types, booleans, and comma-separated array input. Adds writable property derived from AccessLevel bits. Demo shows inline TextField + Write button for writable nodes, "(READ-ONLY)" for others. --- demo/NodePage.qml | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) (limited to 'demo/NodePage.qml') diff --git a/demo/NodePage.qml b/demo/NodePage.qml index 1468817..442a6fd 100644 --- a/demo/NodePage.qml +++ b/demo/NodePage.qml @@ -91,21 +91,28 @@ Page { // Column headers RowLayout { Layout.fillWidth: true - spacing: 0 + Layout.leftMargin: 12 + Layout.rightMargin: 12 + spacing: 12 Label { text: "Identifier" font.bold: true - Layout.preferredWidth: 200 - leftPadding: 12 + Layout.preferredWidth: 160 } Label { text: "Value" font.bold: true + Layout.preferredWidth: 160 + } + Label { + text: "Write" + font.bold: true Layout.fillWidth: true } } Rectangle { + id: separator Layout.fillWidth: true height: 1 color: "#ccc" @@ -128,6 +135,10 @@ Page { id: node nodeId: delegate.modelData monitored: nodePage.StackView.status === StackView.Active + onWriteCompleted: (success, message) => { + var short_id = node.nodeId.substring(node.nodeId.indexOf(";s=") + 3); + nodePage.logFunction(short_id + ": " + message); + } } background: Rectangle { @@ -137,19 +148,45 @@ Page { contentItem: RowLayout { spacing: 12 + + // Column 1: Identifier Label { text: { var idx = node.nodeId.indexOf(";s="); return idx >= 0 ? node.nodeId.substring(idx + 3) : node.nodeId; } - Layout.preferredWidth: 188 + Layout.preferredWidth: 160 elide: Text.ElideRight } + + // Column 2: Live value (always visible) Label { text: node.value !== undefined && String(node.value) !== "" ? String(node.value) : "—" - Layout.fillWidth: true + Layout.preferredWidth: 160 elide: Text.ElideRight } + + // Column 3: Edit area (writable) or READ-ONLY label + TextField { + id: editField + visible: node.writable + Layout.fillWidth: true + placeholderText: "Enter value..." + onAccepted: node.writeValue(text) + } + Button { + id: writeButton + visible: node.writable + text: "Write" + onClicked: node.writeValue(editField.text) + } + Label { + visible: !node.writable + text: "(READ-ONLY)" + color: "gray" + font.italic: true + Layout.fillWidth: true + } } ToolTip.visible: hovered -- cgit v1.2.3