Qt GRPC
Qt GRPC与 Qt Protobuf模块,可与服务器进行快速、节省空间的通信。 gRPC™服务器进行快速、节省空间的通信。它将gRPC 服务无缝集成到 Qt 框架中,允许在不同的编程语言或环境中进行通信,无论服务器是否基于 Qt。
概述
gRPC 是一个跨平台、高性能的远程过程调用(RPC)框架。它使用 Protobuf 编译器 ( ) 从各种编程语言的 protobuf 模式中的 部分生成客户端和服务器接口。它通常用于连接微服务式架构中的服务,或连接移动应用程序和浏览器与后台系统。更多详情,请参阅protoc
service
gRPC 简介。
Qt GRPC gRPC 由 C++ 和 API 组成,使用著名的Qt Quick 信号与插槽机制处理远程过程调用的异步特性。它还提供了命令行工具和 CMake API,可使用 简化 Qt 特定接口的生成。protoc
Qt GRPC Qt 目前支持客户端功能。这意味着它可以与兼容 gRPC 的服务器通信。这可以是你自己的服务器及其 protobuf API,也可以是公开可用的 API,如Google API。
使用模块
使用模块的前提条件
protoc
要运行 Qt 代码生成器插件,需要使用 Protoc、协议缓冲区编译器(Protocol Buffers compiler)。请参阅Protoc 安装。- 可选:支持SSL。
- 可选:用于运行示例的gRPC C++。
注: 有关安装gRPC 和Protobuf 以及运行Qt GRPC 示例的说明,请参阅Windows 下使用 vcpkg 安装的示例。
C++ 应用程序接口
要在基于 Qt 的项目中添加 protobuf 文件并生成所需的源代码,可使用protoc
编译器以及qtgrpcgen
和qtprotobufgen
Qt 插件。
- qtprotobufgen 工具可从 protobuf消息定义生成 Qt 类。使用qt_add_protobufCMake 命令可自动完成这一过程。
- qtgrpcgen 工具从 protobuf服务定义生成接口。使用qt_add_grpcCMake 命令可自动完成此过程。
使用Qt GRPC 的 CMake 项目文件的最小示例如下:
cmake_minimum_required(VERSION 3.16...3.22) project(MyProject) find_package(Qt6 REQUIRED COMPONENTS Protobuf Grpc) qt_standard_project_setup() qt_add_executable(MyApp main.cpp) qt_add_protobuf(MyApp PROTO_FILES path/to/messages.proto ) qt_add_grpc(MyApp CLIENT PROTO_FILES path/to/service.proto ) target_link_libraries(MyApp PRIVATE Qt6::Protobuf Qt6::Grpc)
QML 应用程序接口
模块的 QML 类型可通过QtGrpc
import 获取。要使用这些类型,请在 .qml 文件中添加以下导入语句:
import QtGrpc
文章和指南
引用
示例
许可证和归属
Qt GRPC 模块可在Qt Company 的商业许可下使用。此外,它还受自由软件许可证的保护:GNU 通用公共许可证第 3 版。有关详细信息,请参阅Qt 许可。
此外,Qt 6.9.0 中的Qt GRPC 可能包含以下许可下的第三方模块:
阿帕奇许可证 2.0 |
© 2025 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.