Qt GRPC
Qt GRPC is the client-side implementation that, together with the Qt Protobuf module, allows the definition of messages and services in .proto
files, which then use the provided code generators to generate client code that allows the access for fields and gRPC services in the Qt framework. The code generated by Qt GRPC enables the client-side to communicate with a gRPC server (regardless of whether the server uses Qt) by sending calls or streaming messages.
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 to connect 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
Using the Module
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.
- OpenSSL Toolkit may be needed.
Note: Check the Example of installation for Windows using vcpkg to see how to install gRPC
and Protobuf
and to run Qt GRPC examples.
To start working with the Qt GRPC functionality you should define required services and messages in a .proto
file. See the helloworld.proto
example:
// 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:
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
.
Note: 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.
QML API
The QML types of the module are available through the QtGrpc
import. To use the types, add the following import statement to your .qml file:
import QtGrpc
C++ API
Using a Qt module's C++ API requires linking against the module library, either directly or through other dependencies. Several build tools have dedicated support for this, including CMake and qmake.
Building with CMake
Use the find_package()
command to locate the needed module component in the Qt6
package:
find_package(Qt6 REQUIRED COMPONENTS Grpc)
target_link_libraries(mytarget PRIVATE Qt6::Grpc)
For more details, see the Build with CMake overview.
Articles and Guides
References
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® is a registered trademark of The Linux Foundation. Please see https://grpc.io/ for more information.
Examples
Licenses and Attributions
The Qt GRPC module is available under commercial licenses from The Qt Company. In addition, it is available under free software licenses: The GNU General Public License, version 3. See Qt Licensing for further details.
Furthermore, Qt GRPC in Qt 6.8.1 may contain third party modules under the following permissive licenses:
Apache License 2.0 |
© 2024 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.