Qt GRPC

Qt GRPC 连同 Qt Protobuf module, enables fast and space-efficient communication with gRPC ™ servers. It integrates gRPC services seamlessly into the Qt framework, allowing communication across different programming languages or environments regardless of whether the server is based on Qt.

概述

gRPC is a cross-platform, high-performance Remote Procedure Call (RPC) framework. It uses the Protobuf compiler ( protoc ) to generate client and server interfaces from service sections in a protobuf schema for various programming languages. It is commonly used to connect services in microservices-style architectures or to link mobile applications and browsers with backend systems. For more details, see the gRPC 介绍 .

Qt GRPC consists of C++ and Qt Quick APIs that handle the asynchronous nature of Remote Procedure Calls using the well-known 信号 & 槽 mechanism. It also provides command-line tools and a CMake API to streamline the generation of Qt-specific interfaces using protoc .

Qt GRPC currently supports client-side functionality. This means it can communicate with a gRPC-compatible server. This could be your own server with its protobuf API or a publicly available API, such as the Google API .

使用模块

模块先决条件

  • protoc , the Protocol Buffers compiler, is required to run the Qt code generator plugins. See Protoc Installation .
  • Optional: SSL 支持。
  • Optional: gRPC C++ for running the examples.

注意: 见 Example of installation for Windows using vcpkg for instructions on installing gRPC and Protobuf , and running Qt GRPC examples.

C++ API

To add protobuf files to a Qt-based project and generate the required source code, use the protoc compiler along with the qtgrpcgen and qtprotobufgen Qt plugins.

A minimal example of a CMake project file that uses Qt GRPC can look like this:

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 API

模块的 QML 类型是可用的透过 QtGrpc 导入。要使用类型,添加以下 import 语句到 .qml 文件:

import QtGrpc
					

文章和指南

参考

范例

许可和归属

The Qt GRPC module is available under commercial licenses from Qt 公司 。此外,它在自由软件许可下也是可用的: GNU GPL (一般公共许可) 第 3 版 。见 Qt 许可 进一步了解细节。

Furthermore, Qt GRPC in Qt 6.10.1 may contain third party modules under the following permissive licenses:

gRPC, version 1.50.1

Apache 许可 2.0