From b987ea034adf797b1955748c12b9aeabf1c6c1eb Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Tue, 12 Sep 2017 10:06:20 +0200 Subject: Beautify code of qmlchatclient example Follow http://doc.qt.io/qt-5/qml-codingconventions.html This does not change behavior. Change-Id: Ic0e9aa20e9954cd459c4e0b59b67eadcaebd4e47 Reviewed-by: Milian Wolff --- .../webchannel/chatclient-qml/qmlchatclient.qml | 118 ++++++++++++--------- 1 file changed, 68 insertions(+), 50 deletions(-) (limited to 'examples/webchannel') diff --git a/examples/webchannel/chatclient-qml/qmlchatclient.qml b/examples/webchannel/chatclient-qml/qmlchatclient.qml index ebca1eb..71b8381 100644 --- a/examples/webchannel/chatclient-qml/qmlchatclient.qml +++ b/examples/webchannel/chatclient-qml/qmlchatclient.qml @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Copyright (C) 2016 basysKom GmbH, author Bernd Lamecker ** Contact: https://www.qt.io/licensing/ ** @@ -58,34 +58,40 @@ import "qwebchannel.js" as WebChannel ApplicationWindow { id: root + + property var channel + title: qsTr("Hello World") width: 640 height: 480 - property var channel; - WebSocket { id: socket - url: "ws://localhost:12345"; - active: false - - // the following three properties/functions are required to align the QML WebSocket API with the HTML5 WebSocket API. - property var send: function (arg) { + // the following three properties/functions are required to align the QML WebSocket API + // with the HTML5 WebSocket API. + property var send: function(arg) { sendTextMessage(arg); } onTextMessageReceived: { onmessage({data: message}); } - property var onmessage; + + property var onmessage + + active: false + url: "ws://localhost:12345" onStatusChanged: { - if (socket.status === WebSocket.Error) { + switch (socket.status) { + case WebSocket.Error: console.error("Error: " + socket.errorString); - } else if (socket.status === WebSocket.Closed) { + break; + case WebSocket.Closed: messageBox.text += "\nSocket closed"; - } else if (socket.status === WebSocket.Open) { + break; + case WebSocket.Open: //open the webchannel with the socket as transport new WebChannel.QWebChannel(socket, function(ch) { root.channel = ch; @@ -97,10 +103,12 @@ ApplicationWindow { userlist.text += user + '\n'; }); }); + //connect to the newMessage signal ch.objects.chatserver.newMessage.connect(function(time, user, message) { chat.text = chat.text + "[" + time + "] " + user + ": " + message + '\n'; }); + //connect to the keep alive signal ch.objects.chatserver.keepAlive.connect(function(args) { if (loginName.text !== '') @@ -108,6 +116,7 @@ ApplicationWindow { ch.objects.chatserver.keepAliveResponse(loginName.text); }); }); + break; } } } @@ -116,8 +125,10 @@ ApplicationWindow { id: grid columns: 2 anchors.fill: parent + Text { id: chat + text: "" Layout.fillHeight: true Layout.fillWidth: true @@ -125,20 +136,23 @@ ApplicationWindow { Text { id: userlist + text: "" width: 150 Layout.fillHeight: true } TextField { id: message + height: 50 Layout.columnSpan: 2 Layout.fillWidth: true onEditingFinished: { - if (message.text.length) + if (message.text.length) { //call the sendMessage method to send the message root.channel.objects.chatserver.sendMessage(loginName.text, message.text); + } message.text = ''; } } @@ -146,43 +160,47 @@ ApplicationWindow { Window { - id: loginWindow; - title: "Login"; - modality: Qt.ApplicationModal - - TextField { - id: loginName - - anchors.top: parent.top - anchors.horizontalCenter: parent.horizontalCenter - } - Button { - anchors.top: loginName.bottom - anchors.horizontalCenter: parent.horizontalCenter - id: loginButton - text: "Login" - - onClicked: { - //call the login method - root.channel.objects.chatserver.login(loginName.text, function(arg) { - //check the return value for success - if (arg === true) { - loginError.visible = false; - loginWindow.close(); - } else { - loginError.visible = true; - } - }); - - } - } - Text { - id: loginError - anchors.top: loginButton.bottom - anchors.horizontalCenter: parent.horizontalCenter - text: "Name already in use" - visible: false; - } + id: loginWindow + + title: "Login" + modality: Qt.ApplicationModal + + TextField { + id: loginName + + anchors.top: parent.top + anchors.horizontalCenter: parent.horizontalCenter + } + + Button { + id: loginButton + + anchors.top: loginName.bottom + anchors.horizontalCenter: parent.horizontalCenter + text: "Login" + + onClicked: { + //call the login method + root.channel.objects.chatserver.login(loginName.text, function(arg) { + //check the return value for success + if (arg === true) { + loginError.visible = false; + loginWindow.close(); + } else { + loginError.visible = true; + } + }); + } + } + + Text { + id: loginError + + anchors.top: loginButton.bottom + anchors.horizontalCenter: parent.horizontalCenter + text: "Name already in use" + visible: false + } } Component.onCompleted: { -- cgit v1.2.1