本页内容

QProtobufMessage Class

所有 Protobuf 消息的基类。更多内容...

头文件: #include <QProtobufMessage>
CMake: find_package(Qt6 REQUIRED COMPONENTS Protobuf)
target_link_libraries(mytarget PRIVATE Qt6::Protobuf)
自: Qt 6.5
由以下版本继承:

QtProtobuf::Any

公共函数

(since 6.8) bool deserialize(QAbstractProtobufSerializer *serializer, QByteArrayView data)
QVariant property(QAnyStringView propertyName) const
(since 6.8) const QtProtobufPrivate::QProtobufPropertyOrdering *propertyOrdering() const
(since 6.8) QByteArray serialize(QAbstractProtobufSerializer *serializer) const
bool setProperty(QAnyStringView propertyName, const QVariant &value)
(since 6.6) bool setProperty(QAnyStringView propertyName, QVariant &&value)
(since 6.7) QList<QByteArray> unknownFieldData(qint32 field) const
(since 6.7) QList<qint32> unknownFieldNumbers() const

静态公共成员

QProtobufMessagePointer constructByName(const QString &messageType)

受保护函数

QProtobufMessage(QProtobufMessage &&other)
QProtobufMessagePointer
(since 6.11) To qprotobufmessage_cast(QProtobufMessage *from)
(since 6.11) To qprotobufmessage_cast(const QProtobufMessage *from)

详细说明

提供对消息属性的访问,使用setProperty()和property(),而不依赖于消息的具体类型。

成员函数文档

[constexpr noexcept protected default] QProtobufMessage::QProtobufMessage(QProtobufMessage &&other)

通过Move构造一个QProtobufMessage 实例。

[static] QProtobufMessagePointer QProtobufMessage::constructByName(const QString &messageType)

使用 `messageType` 构造 `QProtobufMessage `。返回所构造的 `QProtobufMessage` 的指针。

该函数尝试创建一个类型与messageType 匹配的消息。如果messageType 未知,则该函数返回nullptr 。如果在注册表中未找到该消息,则该函数返回nullptr 。生成的消息的所有权归调用该函数的一方所有。

[since 6.8] bool QProtobufMessage::deserialize(QAbstractProtobufSerializer *serializer, QByteArrayView data)

使用serializer 从QByteArray data 反序列化此 Protobuf 消息。如果反序列化成功,则返回true ;否则返回false 。

该函数在 Qt 6.8 中引入。

另请参阅 serialize()。

QVariant QProtobufMessage::property(QAnyStringView propertyName) const

获取属性propertyName 的值。

如果未知propertyName ,则返回的QVariant 无效。

另请参阅 setProperty()。

[since 6.8] const QtProtobufPrivate::QProtobufPropertyOrdering *QProtobufMessage::propertyOrdering() const

返回派生 Protobuf 消息的属性排序指针。

该函数于 Qt 6.8 中引入。

[since 6.8] QByteArray QProtobufMessage::serialize(QAbstractProtobufSerializer *serializer) const

使用serializer 将此 Protobuf 消息序列化为QByteArray 。

该函数在 Qt 6.8 中引入。

另请参阅 deserialize()。

bool QProtobufMessage::setProperty(QAnyStringView propertyName, const QVariant &value)

将属性propertyName 设置为存储在value 中的值。

如果 `propertyName ` 不是已知字段,则该值将被忽略,函数返回 `false`。

如果无法将 `value ` 存储到该属性上,则返回 `false `。否则返回 `true`。

另请参阅 property()。

[since 6.6] bool QProtobufMessage::setProperty(QAnyStringView propertyName, QVariant &&value)

这是一个重载函数。

该函数在 Qt 6.6 中引入。

[since 6.7] QList<QByteArray> QProtobufMessage::unknownFieldData(qint32 field) const

返回未知的field 值,并按从网络接收时的顺序进行排序。

该函数于 Qt 6.7 版本中引入。

[since 6.7] QList<qint32> QProtobufMessage::unknownFieldNumbers() const

返回在反序列化过程中QtProtobuf 无法识别的字段编号。

该函数于 Qt 6.7 中引入。

相关的非成员

[alias] QProtobufMessagePointer

是 std::unique_ptr<QProtobufMessage,QProtobufMessageDeleter> 的同义词。用于管理动态分配的 QProtobufMessages 的生命周期,例如通过调用QProtobufMessage::constructByName 创建的那些。

[since 6.11] template <typename To> To qprotobufmessage_cast(QProtobufMessage *from)

[since 6.11] template <typename To> To qprotobufmessage_cast(const QProtobufMessage *from)

如果给定的from 对象类型为To (或其子类),则返回将其强制转换为指针类型To 的对象;否则返回nullptr 。如果from 为nullptr ,则该函数还返回nullptr 。

模板参数To 必须是继承自QProtobufMessage 且使用Q_PROTOBUF_OBJECT 宏声明的类的指针。

示例:

QProtobufMessage *message = new myproject::MySpecificMessage();

// Cast to its own type (success)
auto *specific = qprotobufmessage_cast<myproject::MySpecificMessage *>(message);
// specific is a valid pointer

// Cast to an unrelated type (failure)
auto *another = qprotobufmessage_cast<myproject::AnotherMessage *>(message);
// another is nullptr

该函数的行为与标准 C++ 的dynamic_cast() 类似,其优点在于无需 RTTI 支持,且可在动态库边界之间正常工作。

警告:如果 `To ` 所指向的类型未通过 `Q_PROTOBUF_OBJECT ` 宏进行声明,则该函数的返回值未定义。

这些函数在 Qt 6.11 中引入。

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