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.
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
protoc
, the Google protocol buffers compiler, must be installed to generate code from
.proto
specification files. See
Protoc Installation
.
注意:
检查
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
范例:
// 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.
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
.
注意:
若
.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 类型是可用的透过
QtGrpc
导入。要使用类型,添加以下 import 语句到 .qml 文件:
import QtGrpc
使用 Qt 模块的 C++ API 要求直接 (或透过其它依赖) 链接到模块库。几个构建工具对此有专用支持,包括 CMake and qmake .
使用
find_package()
命令定位所需模块组件在
Qt6
包:
find_package(Qt6 REQUIRED COMPONENTS Grpc)
target_link_libraries(mytarget PRIVATE Qt6::Grpc)
更多细节,见 构建采用 CMake 概述。
Qt GRPC is available under commercial licenses from Qt 公司 。此外,它是可用的根据 GNU GPL (一般公共许可) 第 3 版 。见 Qt 许可 进一步了解细节。
gRPC ® is a registered trademark of The Linux Foundation. Please see https://grpc.io/ 了解更多信息。
The Qt GRPC module is available under commercial licenses from Qt 公司 。此外,它在自由软件许可下也是可用的: GNU GPL (一般公共许可) 第 3 版 。见 Qt 许可 进一步了解细节。
Furthermore, Qt GRPC in Qt 6.7.2 may contain third party modules under the following permissive licenses:
Apache 许可 2.0 |