From 3b74fdff66c4f29eabf1b9ca19e89e4202d1d80f Mon Sep 17 00:00:00 2001 From: Tatiana Borisova Date: Tue, 25 Jul 2023 14:49:04 +0200 Subject: [PATCH] magic8ball example: extend QML support Update magic8ball example using enabled QtGrpc API in QMl. Task-number: QTBUG-114893 Change-Id: I055d20354d8260480af1b8b0d741077734117e75 Reviewed-by: Alexey Edelev --- examples/grpc/magic8ball/CMakeLists.txt | 10 ++--- examples/grpc/magic8ball/Main.qml | 47 +++++++++++++++++----- examples/grpc/magic8ball/clientservice.cpp | 46 --------------------- examples/grpc/magic8ball/clientservice.h | 36 ----------------- examples/grpc/magic8ball/main.cpp | 2 - 5 files changed, 42 insertions(+), 99 deletions(-) delete mode 100644 examples/grpc/magic8ball/clientservice.cpp delete mode 100644 examples/grpc/magic8ball/clientservice.h diff --git a/examples/grpc/magic8ball/CMakeLists.txt b/examples/grpc/magic8ball/CMakeLists.txt index 642be0d3..143e4273 100644 --- a/examples/grpc/magic8ball/CMakeLists.txt +++ b/examples/grpc/magic8ball/CMakeLists.txt @@ -17,14 +17,16 @@ qt_add_executable(magic8ball main.cpp ) -qt_add_protobuf(magic8ball +qt_add_protobuf(magic8ball_plugin PROTO_FILES proto/exampleservice.proto + QML ) -qt_add_grpc(magic8ball CLIENT +qt_add_grpc(magic8ball_plugin CLIENT PROTO_FILES proto/exampleservice.proto + QML ) set_target_properties(magic8ball PROPERTIES @@ -36,9 +38,6 @@ qt_add_qml_module(magic8ball URI qtgrpc.examples.magic8ball VERSION 1.0 RESOURCE_PREFIX "/qt/qml" - SOURCES - clientservice.h - clientservice.cpp QML_FILES "WaitingAnimation.qml" "AnimatedAnswer.qml" @@ -52,6 +51,7 @@ target_link_libraries(magic8ball PRIVATE Qt6::Quick Qt6::Protobuf Qt6::Grpc + Qt6::GrpcQuick ) install(TARGETS magic8ball diff --git a/examples/grpc/magic8ball/Main.qml b/examples/grpc/magic8ball/Main.qml index a8e3e13d..c991b30c 100644 --- a/examples/grpc/magic8ball/Main.qml +++ b/examples/grpc/magic8ball/Main.qml @@ -2,10 +2,12 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import QtQuick +import QtGrpcQuick import QtQuick.Controls import QtQuick.Controls.Material import QtQuick.Shapes +import qtgrpc.examples import qtgrpc.examples.magic8ball ApplicationWindow { @@ -23,6 +25,12 @@ ApplicationWindow { property string textAnswer: "" property string textError: "" + property answerRequest _answerReq + property answerResponse _answerResp + + property var setResponse: function(value) { root._answerResp = value } + property var errorCallback: function() { console.log("Error can be handled also here") } + MagicText { anchors.top: parent.top anchors.topMargin: 20 @@ -128,13 +136,9 @@ ApplicationWindow { } Connections { - target: ClientService - function onMessageRecieved(value) { - root.textAnswer = value - } - - function onErrorRecieved(value) { - root.textError = value + target: grpcClient + function onErrorOccurred() { + root.textError = "No connection\nto\nserver" } } @@ -189,15 +193,16 @@ ApplicationWindow { Timer { id: animationTimeout - interval: 3000 + interval: 5000 repeat: false running: false - onTriggered: ClientService.setMessage() + onTriggered: root.textAnswer = _answerResp.message onRunningChanged: { if (running) { + root.textError = "" answer.closingAnimation.start() - ClientService.sendRequest() + root.sendRequest() } else { waitingAnimation.runAnimation = false waitingAnimation.visible = false @@ -206,7 +211,29 @@ ApplicationWindow { } } + function sendRequest() + { + grpcClient.answerMethod(_answerReq, setResponse, errorCallback) + } + footer: MagicText { text: root.textError === "" ? "" : "Please, start server: ../magic8ball/SimpleGrpcServer" } + + QmlClient { + id: grpcClient + channel: grpcChannel.channel + } + + GrpcHttp2Channel { + id: grpcChannel + options: GrpcChannelOptions { + id: channelOptions + host: "http://localhost:50051" + } + } + + Component.onCompleted: { + _answerReq.message = "sleep" + } } diff --git a/examples/grpc/magic8ball/clientservice.cpp b/examples/grpc/magic8ball/clientservice.cpp deleted file mode 100644 index 9274a09b..00000000 --- a/examples/grpc/magic8ball/clientservice.cpp +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (C) 2023 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only - -#include "clientservice.h" - -#include - -using namespace qtgrpc::examples; - -ClientService::ClientService(QObject *parent) : - QObject(parent), - m_client(new ExampleService::Client), - m_response(new AnswerResponse) -{ - connect(m_client.get(), &ExampleService::Client::errorOccurred, - this, &ClientService::errorOccurred); - - //! [0] - QGrpcChannelOptions channelOptions(QUrl("http://localhost:50051", QUrl::StrictMode)); - m_client->attachChannel(std::make_shared(channelOptions)); - //! [0] -} - -void ClientService::errorOccurred() -{ - qWarning() << "Connection error occurred. Have you started server part:" - " ../magic8ball/SimpleGrpcServer?"; - - emit errorRecieved("No connection\nto\nserver"); -} - -void ClientService::sendRequest() -{ - // clean error on UI before new request - emit errorRecieved(""); - - AnswerRequest request; - request.setMessage("sleep"); - - m_client->answerMethod(request, m_response.get()); -} - -void ClientService::setMessage() -{ - emit messageRecieved(m_response.get()->message()); -} diff --git a/examples/grpc/magic8ball/clientservice.h b/examples/grpc/magic8ball/clientservice.h deleted file mode 100644 index 68e67264..00000000 --- a/examples/grpc/magic8ball/clientservice.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (C) 2023 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only - -#ifndef CLIENT_SERVICE_H -#define CLIENT_SERVICE_H - -#include -#include -#include - -#include - -#include "exampleservice.qpb.h" -#include "exampleservice_client.grpc.qpb.h" - -class ClientService : public QObject -{ - Q_OBJECT - QML_ELEMENT - QML_SINGLETON -public: - explicit ClientService(QObject *parent = nullptr); - Q_INVOKABLE void sendRequest(); - Q_INVOKABLE void setMessage(); - void errorOccurred(); - -signals: - void messageRecieved(const QString &value); - void errorRecieved(const QString &value); - -private: - std::unique_ptr m_client; - std::unique_ptr m_response; -}; - -#endif // CLIENT_SERVICE_H diff --git a/examples/grpc/magic8ball/main.cpp b/examples/grpc/magic8ball/main.cpp index 078995d1..0853b429 100644 --- a/examples/grpc/magic8ball/main.cpp +++ b/examples/grpc/magic8ball/main.cpp @@ -1,8 +1,6 @@ // Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only -#include "clientservice.h" - #include #include