Magic 8 Ball
Creazione di una connessione HTTP2 tra un client Qt gRPC™ e un server gRPC in C++.
Magic 8 Ball invia una domanda a un server e visualizza la risposta ricevuta:

Il codice di esempio include i seguenti componenti:
magic8ballUn'applicazione client Qt gRPC che utilizza le funzioni CMake qt_add_protobuf() e qt_add_grpc() per la generazione del codice Qt relativo ai messaggi e ai servizi.serverApplicazione che richiama il plugin C++ gRPC per generare il codice del server e implementare una semplice logica di server.
Nota: è necessarioche il plugin C++ gRPC sia installato. Maggiori dettagli sono disponibili qui: Prerequisiti del modulo
Entrambe le componenti utilizzano messaggi generati dallo schema protobuf descritto nel file exampleservice.proto:
syntax = "proto3";
package qtgrpc.examples;
message AnswerRequest {
string question = 1;
}
message AnswerResponse {
string message = 1;
}
service ExampleService {
rpc answerMethod(AnswerRequest) returns (AnswerResponse) {}
}Il client gRPC è definito come un oggetto QML disponibile dopo la compilazione del codice.
ExampleServiceClient {
id: grpcClient
channel: grpcChannel.channel
}Il servizio client si connette all’ localhost sulla porta 50051, specificata nelle opzioni del canale gRPC:
GrpcHttp2Channel {
id: grpcChannel
hostUri: "http://localhost:50051"
// Optionally, you can specify custom channel options here
// options: GrpcChannelOptions {}
}E invia una richiesta alla parte server:
function requestAnswer(question: string): void {
...
root.answerReq.question = question;
grpcClient.answerMethod(root.answerReq, finishCallback, errorCallback, grpcCallOptions);
}answerMethod è un metodo di gRPC che il client chiama. Ha quattro parametri: l’oggetto della richiesta, una funzione di callback di completamento, una funzione di callback di errore e un oggetto GrpcCallOptions.
Fare clic sul pulsante « Ask » per inviare la richiesta al server magic8ball.
Nota: è necessario eseguire il server in parallelo con l’applicazione client.
L'applicazione server sceglie una risposta casuale dall'elenco delle risposte e invia i dati alla porta del client. Verifica inoltre che la richiesta contenga un campo question non vuoto. Se il campo è vuoto, restituisce un StatusCode::INVALID_ARGUMENT
grpc::Status answerMethod(grpc::ServerContext *, const AnswerRequest *request,
AnswerResponse *response) override
{
if (request->question().empty()) {
std::cerr << "Question is empty" << std::endl;
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Question is empty");
}
std::cout << "Received question: " << request->question() << std::endl;
response->set_message(getRandomAnswer());
return grpc::Status();
};Dopo aver ricevuto una risposta, l’applicazione client visualizza la risposta.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.