mirror of https://github.com/qt/qtgrpc.git
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 <alexey.edelev@qt.io>
This commit is contained in:
parent
42ce3ecdc5
commit
3b74fdff66
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 <QGrpcHttp2Channel>
|
||||
|
||||
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<QGrpcHttp2Channel>(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());
|
||||
}
|
||||
|
|
@ -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 <QObject>
|
||||
#include <QString>
|
||||
#include <qqmlregistration.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#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<qtgrpc::examples::ExampleService::Client> m_client;
|
||||
std::unique_ptr<qtgrpc::examples::AnswerResponse> m_response;
|
||||
};
|
||||
|
||||
#endif // CLIENT_SERVICE_H
|
||||
|
|
@ -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 <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue