Protobuf Qt Core Types Module

为在 protobuf 中使用本地Qt Core 类型提供支持。更多

此模块在 Qt 6.6 中引入。

详细说明

Qt::ProtobufQtCoreTypes 是一个具有预定义 protobuf 消息的库,旨在封装一系列Qt Core 类型。

支持的Qt Core 类型列表:

Qt Core 用法

要启用Qt Core 类型支持,请将 ProtobufQtCoreTypes 作为依赖项添加到 CMake 项目中:

...
find_package(Qt REQUIRED COMPONENTS Protobuf ProtobufQtCoreTypes)
... # After target creation
target_link_libraries(${TARGET} PRIVATE Qt::ProtobufQtCoreTypes)

在对使用Qt Core 类型作为字段的消息进行序列化或反序列化之前,请调用注册方法:

// e.g. in main.cpp
QtProtobuf::qRegisterProtobufQtCoreTypes();
...

所有支持的消息都在一个特殊的 .proto 文件QtCore.proto 中描述,该文件描述了来自Qt Core 模块的 Qt 类型。

您也可以使用该文件为其他语言或框架生成代码。

在界面 .proto 文件中导入所需的Qt Core 类型模块。例如

syntax = "proto3";

package project.module.component;

import "QtCore/QtCore.proto";

message QUrlMessage {
    QtCore.QUrl url = 1;
}

Qt Protobuf 生成器会检测由 ProtobufQtCoreTypes 包提供的Qt Core 类型,并直接使用它们。这样就能灵活使用这些类型,而无需额外转换。

在 CMake 中,你可以使用QT_PROTO_INCLUDES target 属性从库中提取 proto 包含路径:

qt_add_protobuf(MyProtoLib
    PROTO_FILES
        MyQtCoreTypesUsage.proto
    PROTO_INCLUDES
        $<TARGET_PROPERTY:Qt::ProtobufQtCoreTypes,QT_PROTO_INCLUDES>
)

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