qtprotobufgen工具

The qtprotobufgen Tool

qtprotobufgen工具

The qtprotobufgen tool can be used to generate QtProtobuf classes from a protobuf schema. The tool is provided by the CMake Qt6::ProtobufTools package. It works as an extension to Google's protoc tool.

​qtprotobufgen工具可用于从protobuf模式生成QtProtobuf类。该工具由CMake Qt6::ProtobufTools软件包提供。它是对谷歌协议工具的扩展。

find_package(Qt6 COMPONENTS ProtobufTools REQUIRED)

Usage

用法

Qt provides CMake functions that ease the use of the qtprotobufgen tool. When using CMake as a build tool you should prefer using the Qt CMake API. For build systems other than CMake, adapt the commands described in Running manually.

​Qt提供了简化qtprotobufgen工具使用的CMake函数。当使用CMake作为构建工具时,应该更喜欢使用Qt-CMake API。对于CMake以外的构建系统,请调整手动运行中所述的命令。

Note: there is no explicit support for building gRPC and Protobuf applications using the Qt GRPC module with qmake.

注意:没有明确支持使用带qmake的Qt gRPC模块构建gRPC和Protobuf应用程序。

CMake

The following CMake commands integrate a protobuf schema into a Qt project.

以下CMake命令将protobuf模式集成到Qt项目中。 

qt_add_protobuf

Generates Qt-based C++ source code using a protobuf schema

使用protobuf模式生成基于Qt的C++源代码

Usually qtprotobufgen would be invoked through CMake using the qt_add_protobuf macro, as shown in the following examples:

通常,qtprotobufgen将使用qt_add_protobuf宏通过CMake调用,如以下示例所示:

cmake_minimum_required(VERSION 3.16...3.22)
project(MyThings)

find_package(Qt6 REQUIRED COMPONENTS Protobuf)
qt_standard_project_setup()

qt_add_protobuf(MyMessages
    GENERATE_PACKAGE_SUBFOLDERS
    PROTO_FILES
        path/to/message.proto
        path/to/other_message.proto
    PROTO_INCLUDES
        /path/to/proto/include
)

qt_add_executable(MyApp main.cpp)

target_link_libraries(MyApp PRIVATE MyMessages)

In the example above, we generate a library called MyMessages, which contains the message types defined in the paths passed to the PROTO_FILES option. The GENERATE_PACKAGE_SUBFOLDERS option to generate a folder structure for the generated files. And the PROTO_INCLUDES option tells protoc to look for dependencies or imports in the specified directories. We create a target for an executable called MyApp, which we link to the MyMessages library.

在上面的示例中,我们生成了一个名为MyMessages的库,其中包含传递给PROTO_FILES选项的路径中定义的消息类型。GENERATE_PACKAGE_SUBFOLDERS选项为生成的文件生成文件夹结构。PROTO_INCLUDES选项告诉协议在指定目录中查找依赖项或导入。我们为名为MyApp的可执行文件创建一个目标,并将其链接到MyMessages库。

QML extended protobuf example:

QML扩展protobuf示例:

cmake_minimum_required(VERSION 3.16...3.22)
project(MyThings)

find_package(Qt6 REQUIRED COMPONENTS Protobuf Quick)
qt_standard_project_setup()

qt_add_protobuf(MyMessagesPlugin
    QML
    QML_URI my.messages.module.uri
    PROTO_FILES
        path/to/message.proto
        path/to/other_message.proto
    PROTO_INCLUDES
        /path/to/proto/include
)

qt_add_qml_module(MyApp
    URI example.uri
    VERSION 1.0
    QML_FILES qml/main.qml
)

qt_add_executable(MyApp main.cpp)
target_link_libraries(MyApp PRIVATE Quick)

In the QML extended example above, we generate a QML module called MyMessagesPlugin, which contains the message types defined in the paths passed to the PROTO_FILES option. We use the QML option, that enables proto message types registration in the QML context. The registered types will be available in QML by importing a path that is set by the QML_URI. Finally, we create a target for an executable called MyApp, which has a QML module for the graphical part and loads MyMessagesPlugin into the main.qml file via the my.messages.module.uri import.

在上面的QML扩展示例中,我们生成了一个名为MyMessagesPlugin的QML模块,该模块包含传递给PROTO_FILES选项的路径中定义的消息类型。我们使用QML选项,该选项允许在QML上下文中注册原型消息类型。通过导入QML_URI设置的路径,已注册的类型将在QML中可用。最后,我们为名为MyApp的可执行文件创建一个目标,该可执行文件具有用于图形部分的QML模块,并通过my.messages.module.uri导入将MyMessagesPlugin加载到main.QML文件中。

Running manually

手动运行

protoc --plugin=protoc-gen-qtprotobuf=<path/to/bin/>qtprotobufgen \
    --qtprotobuf_out="[<options>:]<output_dir>" \
    [--qtprotobuf_opt="<options>"] \
    [-I/extra/proto/include/path] \
    <protofile>.proto

The options argument is a semicolon-separated list of Options. It can be passed in two different ways. Either by prepending to the options to the output_dir argument, delimited by a colon. Or through a separate argument, --qtprotobuf_opt. You also can pass the corresponding keys as the QT_PROTOBUF_OPTIONS environment variable. Keys need to be presented as a semicolon-separated list:

​options参数是以分号分隔的options列表。它可以通过两种不同的方式传递。或者在output_dir参数的选项前面加上前缀,用冒号分隔。或者通过一个单独的参数--qtprotobuf_opt。也可以将相应的键作为QT_PROTOBUF_OPTIONS环境变量传递。键需要以分号分隔的列表形式显示:

export QT_PROTOBUF_OPTIONS="COPY_COMMENTS;GENERATE_PACKAGE_SUBFOLDERS;EXTRA_NAMESPACE=MyTopLevelNamespace"

Options

选项

The generator supports options that can be provided to tune generation. Options have direct aliases in the qt_add_protobuf function. The following options are supported:

​生成器支持可用于调整生成的选项。选项在qt_add_protobuf函数中具有直接别名。支持以下选项:

  • COPY_COMMENTS copies comments from .proto files. If provided in the parameter list, comments related to messages and fields are copied to generated header files.
  • COPY_COMMENTS从.proto文件复制注释。如果在参数列表中提供,则与消息和字段相关的注释将复制到生成的头文件中。
  • GENERATE_PACKAGE_SUBFOLDERS generates a folder structure for the generated files matching the .proto file's package name. For example, package io.qt.test; would put the generated files into io/qt/test/.
  • GENERATE_PACKAGE_SUBFOLDERS为生成的与.proto文件包名称匹配的文件生成文件夹结构。例如,包io.qt.test;将生成的文件放入io/qt/test/中。
  • EXTRA_NAMESPACE is an optional namespace that will be used for the generated classes. The classes are always generated in a namespace whose name is the same as the package name specified in the .proto file. If this option is used, then everything will be nested inside the extra namespace.
  • EXTRA_NAMESPACE是一个可选的命名空间,将用于生成的类。类总是在名称与.proto文件中指定的包名称相同的命名空间中生成。如果使用此选项,则所有内容都将嵌套在额外的命名空间中。
  • EXPORT_MACRO is the base name of the symbol export macro used for the generated code. The generated macro name is constructed as QPB_<EXPORT_MACRO>_EXPORT. If the option is not set, the macro is not generated.
  • EXPORT_MACRO是用于生成代码的符号导出宏的基本名称。生成的宏名称构造为QPB_<EXPORTMACRO>_EXPORT。如果未设置该选项,则不会生成宏。
  • QML_URI enables QProtobufMessage types in the QML context by registering them in a QML module via the provided URI import path. The URI option will be used in the line of the generated qmldir file, and also used to form the target path by replacing dots with forward slashes.
  • ​QML_URI通过提供的URI导入路径在QML模块中注册QProtobufMessage类型,从而在QML上下文中启用QProtobufMessage类型。URI选项将用于生成的qmldir文件的行中,还用于通过用正斜杠替换点来形成目标路径。

Note: Read Identified Modules for further in-depth discussion of the URI.

注意:请阅读已识别模块以了解有关URI的进一步深入讨论。

© 2023 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值