IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Qt GRPC

Provides support for communicating with gRPC services.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Qt GRPC

The Qt GRPC and Qt Protobuf modules together allow you to define data and messages in proto files, and then use the code generators, which generate code allowing accessors for fields and gRPC services in the Qt framework. Such code allows users to communicate with the server by sending calls or stream messages.

I. Overview

gRPC is a cross-platform high performance Remote Procedure Call (RPC) framework, that generates client/server bindings for a lot of languages. Usually, you use it to connect services in a microservices-style architecture or connecting mobile applications and browsers to backend services. The gRPC clients and servers can run and talk to each other in various environments, and you can write in any of gRPC’s supported languages. For more details see gRPC Introduction

II. Using the Module

 

II-1. Module prerequisites:

  • protoc, the Google protocol buffers compiler, must be installed to generate code from .proto specification files. See Protoc Installation.

  • If you also install the gRPC libraries, you'll be able to use native gRPC channels. See gRPC for C++ for details.

To start working with the Qt GRPC functionality you should define required services and messages in a .proto file. See the helloworld.proto example:

 
Sélectionnez
// The service definition.
service Salutation {
  // Sends a greeting
  rpc SendHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

To add a .proto file to a Qt-based project and generate the required source code, you should use the protoc compiler with the qtgrpcgen and qtprotobufgen Qt plugins.

  • The Qt Protobuf plugin generates classes that you can use to serialize and deserialize their associated protobuf messages.

  • The Qt GRPC plugin generates gRPC client classes from the proto file.

This processing of proto files into source code can be automated in CMake using the following commands provided by Qt:

As a result, the full example of a CMake project file, that uses Qt GRPC functionality shall be:

 
Sélectionnez
cmake_minimum_required(VERSION 3.16...3.22)
project(MyProject)

find_package(Qt6 REQUIRED COMPONENTS Protobuf Grpc)
qt_standard_project_setup()

qt_add_protobuf(MyProtoMessageLib
    PROTO_FILES
        path/to/helloworld.proto
    PROTO_INCLUDES
        path/to/proto/include
)

qt_add_grpc(MyGrpcClient CLIENT
    PROTO_FILES
        path/to/helloworld.proto
    PROTO_INCLUDES
        path/to/proto/include
)

qt_add_executable(MyApp main.cpp)

target_link_libraries(MyApp PRIVATE MyGrpcClient MyProtoMessageLib Qt6::Protobuf)

The example above calls the qt_add_grpc() CMake function to generate a library called MyGrpcClient.

if the .proto file API contains messages, then the qt_add_protobuf() CMake function should be called to generate protobuf message classes for the project.

Finally, the example creates a target for an executable called MyApp which links to the MyGrpcClient and MyProtoMessageLib libraries.

III. Class Documentation

IV. CMake API

V. Licenses and Trademarks

Qt GRPC is available under commercial licenses from The Qt Company. In addition, it is available under the GNU General Public License, version 3. See Qt Licensing for further details.

gRPC\sup{®} is a registered trademark of The Linux Foundation. Please see https://grpc.io/ for more information.

VI. Examples

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+