import QtQuick 2.8 import QtQuick.Window 2.2 import QtQuick.Controls 2.1 import QtQuick.Layouts 1.1 import MqttClient 1.0 Rectangle { visible: true width: 1280 height: 800 id: page_mqttclient color: "#498ff8" radius: 8 property var tempSubscription: 0 MqttClient { id: client_mqtt hostname: hostnameField.text port: portField.text } ListModel { id: messageModel } function addMessage(payload) { messageModel.insert(0, {"payload" : payload}) if (messageModel.count >= 100) messageModel.remove(99) } GridLayout { anchors.fill: parent anchors.margins: 10 columns: 2 Label { text: "Hostname:" enabled: client_mqtt.state === MqttClient.Disconnected } TextField { id: hostnameField Layout.fillWidth: true text: "120.27.222.26" placeholderText: "" enabled: client_mqtt.state === MqttClient.Disconnected } Label { text: "Port:" enabled: client_mqtt.state === MqttClient.Disconnected } TextField { id: portField Layout.fillWidth: true text: "1883" placeholderText: "" inputMethodHints: Qt.ImhDigitsOnly enabled: client_mqtt.state === MqttClient.Disconnected } Button { id: connectButton Layout.columnSpan: 2 Layout.fillWidth: true text: client_mqtt.state === MqttClient.Connected ? "Disconnect" : "Connect" onClicked: { if (client_mqtt.state === MqttClient.Connected) { client_mqtt.disconnectFromHost() messageModel.clear() tempSubscription.destroy() tempSubscription = 0 } else client_mqtt.connectToHost() } } RowLayout { enabled: client_mqtt.state === MqttClient.Connected Layout.columnSpan: 2 Layout.fillWidth: true Label { text: "Topic:" } TextField { id: subField placeholderText: "" Layout.fillWidth: true enabled: tempSubscription === 0 } Button { id: subButton text: "Subscribe" visible: tempSubscription === 0 onClicked: { if (subField.text.length === 0) { console.log("No topic specified to subscribe to.") return } tempSubscription = client_mqtt.subscribe(subField.text) tempSubscription.messageReceived.connect(addMessage) } } } ListView { id: messageView model: messageModel height: 300 width: 200 Layout.columnSpan: 2 Layout.fillHeight: true Layout.fillWidth: true clip: true delegate: Rectangle { width: messageView.width height: 30 color: index % 2 ? "#DDDDDD" : "#888888" radius: 5 Text { text: payload anchors.centerIn: parent } } } Label { function stateToString(value) { if (value === 0) return "Disconnected" else if (value === 1) return "Connecting" else if (value === 2) return "Connected" else return "Unknown" } Layout.columnSpan: 2 Layout.fillWidth: true color: "#333333" text: "Status:" + stateToString(client_mqtt.state) + "(" + client_mqtt.state + ")" enabled: client_mqtt.state === MqttClient.Connected } } }