Qt Protobuf

Qt Protobuf 生成器插件 生成基於 Qt 的類從 .proto 文件。可以使用這些類,來序列化和反序列化其關聯的 Protobuf 消息。

概述

什麼是協議緩衝?

Protocol buffers are a language-neutral, platform-neutral, and extensible mechanism for serializing structured data, which is compact and easy to use:

  • A user defines the structure of data in .proto 文件。
  • Proto compiler is invoked at build time on .proto files to generate code in various programming languages.

The provided format is suitable for network traffic or long-term data storage.

協議緩衝解決瞭哪些問題?

Protocol buffers provide a serialization format for typed and structured data packets up to a few megabytes in size. It allows for the extension with new information without invalidating existing data or requiring code to be updated. For more information, see Protocol Buffers Overview .

使用 Qt Protobuf 的好處

You can use a Protocol buffers mechanism in any Qt-based project:

  • Without the need to call a protocol buffer compiler explicitly.
  • Without manually integrating generated code into the project tree.
  • To access the serialization and deserialization API.
  • For integrated properties mechanism for generated protobuf messages.
  • For integrated gRPC-client support, see Qt GRPC module .
  • For integrated QML-types support for generated protobuf messages.
  • For using enabled Qt Core and Qt GUI types as a part of *.proto schema.

使用模塊

Google protocol buffers provide a generator called protoc that supports extensions. qtprotobufgen 工具 is an extension for protoc and requires that you install protoc on your host system. For more information about the installation, see Protoc Installation .

QML API

The QML types of the module are available through the QML and QML_URI options in the qt_add_protobuf macro. The import name is equal to the .proto package name or defined by QML_URI option in the qt_add_protobuf macro. To use the types, add the following import statement to your .qml file:

import my.proto.package.uri
					

C++ API

使用 Qt 模塊的 C++ API 要求直接 (或透過其它依賴) 鏈接到模塊庫。幾個構建工具對此有專用支持,包括 CMake and qmake .

構建采用 CMake

使用 find_package() 命令定位所需模塊組件在 Qt6 包:

find_package(Qt6 REQUIRED COMPONENTS Protobuf)
target_link_libraries(mytarget PRIVATE Qt6::Protobuf)
					

更多細節,見 構建采用 CMake 概述。

文章和指南

參考

範例

許可和歸屬

Qt Protobuf 模塊在商業許可下是可用的來自 Qt 公司 。此外,它在自由軟件許可下也是可用的: GNU LGPL (次一般公共許可) 第 3 版 ,或 GNU GPL (一般公共許可) 第 2 版 ,或 GNU GPL (一般公共許可) 第 3 版 。見 Qt 許可 進一步瞭解細節。

此外, qtprotobufgen tool in Qt 6.8.5 may contain third party modules under the following permissive licenses:

protobuf 第 21.9 版

BSD 3 條款新 (或修訂) 許可