本页内容

Protobuf Qt GUI Types Module

支持在 Protobuf 中使用原生 Qt GUI 类型。更多内容...

该模块于 Qt 6.6 版本中引入。

详细说明

Qt::ProtobufQtGuiTypes 是一个包含预定义 Protobuf 消息的库,旨在封装部分 Qt GUI 类型。

支持的 Qt GUI 类型列表:

Qt GUI 用法

要启用对Qt GUI 类型的支持,请将ProtobufQtGuiTypes作为依赖项添加到您的CMake项目中:

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

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

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

所有受支持的消息都在一个特殊的 .proto 文件(QtGui.proto)中进行了描述,该文件定义了来自Qt GUI 模块的 Qt 类型。

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

在您的接口 .proto 文件中导入所需的Qt GUI 类型模块。例如:

syntax = "proto3";

package project.module.component;

import "QtGui/QtGui.proto";

message QMatrix4x4Message {
    QtGui.QMatrix4x4 matrix = 1;
}

Qt Protobuf 生成器会检测由 ProtobufQtGuiTypes 包提供的Qt GUI 类型,并直接使用它们。这使您能够灵活地使用这些类型,而无需进行额外的转换。

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

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

QImage 序列化

ProtobufQtGuiTypes 库支持与QImage 类进行交互。 通常,该库会将所有QImage 对象转换为PNG 格式。但如果QImage 是使用QImage::Format 中的某种浮点格式创建的,那么转换为PNG 会导致数据丢失。为防止数据丢失,如果系统支持,该库会自动将图像文件转换为TIFF 格式;否则,则转换为PNG 格式。

注意:若 不希望使用TIFF ,则需显式地将图像转换为非浮点格式。

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