Protobuf Qt Core Types Module
支持在 Protobuf 中使用原生Qt Core 类型。更多内容...
该模块于 Qt 6.6 中引入。
详细说明
Qt::ProtobufQtCoreTypes 是一个包含预定义 Protobuf 消息的库,旨在封装精选的Qt Core 类型。
支持的Qt Core 类型列表:
- QByteArray
注意:类型 对应于 protobuf中的字节类型。
- QChar
- QDate
- QTimeZone
- QDateTime
- QPoint
- QPointF
- QRect
- QRectF
- QSize
- QSizeF
- QString
注意:Type 反映的是字节型Protobuf 类型。
- QTime
- QUrl
- QUuid
- QVersionNumber
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 目标属性从库中提取 proto 包含路径:
qt_add_protobuf(MyProtoLib
PROTO_FILES
MyQtCoreTypesUsage.proto
PROTO_INCLUDES
$<TARGET_PROPERTY:Qt::ProtobufQtCoreTypes,QT_PROTO_INCLUDES>
)© 2026 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.