QtProtobuf::Any Class
class QtProtobuf::Any一个辅助类,用于简化对 Protobuf Any 类型的操作。更多内容...
| 头文件: | #include <qprotobufanysupport.h> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS ProtobufWellKnownTypes) target_link_libraries(mytarget PRIVATE Qt6::ProtobufWellKnownTypes) |
| qmake: | QT += protobufwellknowntypes |
| 自: | Qt 6.6 |
| 继承自: | QProtobufMessage |
QtProtobuf::Any 比较
| 类别 | 可比较类型 |
|---|---|
| 相等性 | QtProtobuf::Any |
属性
公共函数
| Any() | |
| Any(const QtProtobuf::Any &other) | |
| Any(QtProtobuf::Any &&other) | |
| ~Any() | |
| void | setTypeUrl(const QString &typeUrl) |
| void | setValue(const QByteArray &value) |
| QString | typeUrl() const |
| std::optional<T> | unpack(QAbstractProtobufSerializer *serializer) const |
| QByteArray | value() const |
| QtProtobuf::Any & | operator=(QtProtobuf::Any &&other) |
| QtProtobuf::Any & | operator=(const QtProtobuf::Any &other) |
静态公共成员
| QtProtobuf::Any | fromMessage(QAbstractProtobufSerializer *serializer, const T &message, QAnyStringView typeUrlPrefix) |
相关的非成员
| bool | operator!=(const QtProtobuf::Any &lhs, const QtProtobuf::Any &rhs) |
| bool | operator==(const QtProtobuf::Any &lhs, const QtProtobuf::Any &rhs) |
详细说明
Any 类支持使用 as() 和fromMessage() 轻松地与其他类型进行类型转换。
当消息包含google.protobuf.Any 类型的字段时,这是qtprotobufgen生成的代码中使用的类型。
属性文档
typeUrl : QString
该属性存储 Any 对象的类型 URL。该 URL 旨在提示所包含数据的真实类型。
注意:Qt 不支持动态获取任何潜在的类型分解规则,而是直接使用类型 URL 来验证传递给 `as<T>() ` 作为 `T` 的类型是否正确。
访问函数:
| QString | typeUrl() const |
| void | setTypeUrl(const QString &typeUrl) |
另请参阅 value 。
value : QByteArray
该属性包含构成 Any 对象中存储值的原始字节。
请参阅typeUrl 以了解如何解释这些字节。
访问函数:
另请参阅 typeUrl 。
成员函数文档
Any::Any()
创建一个初始化为默认状态的空 Any 实例。
Any::Any(const QtProtobuf::Any &other)
创建other 的副本。
[constexpr noexcept default] Any::Any(QtProtobuf::Any &&other)
通过Move构造一个Any 的实例。
[noexcept] Any::~Any()
销毁该 Any 实例
[static] template <typename T> QtProtobuf::Any Any::fromMessage(QAbstractProtobufSerializer *serializer, const T &message, QAnyStringView typeUrlPrefix)
该函数将给定的message 序列化为返回的Any实例的值。该实例的typeUrl()方法由前缀、正斜杠以及通过serializer 从QProtobufMessage::propertyOrdering 获取的消息名称构建而成。如果提供了typeUrlPrefix ,则将其用作前缀;否则使用"type.googleapis.com" 。
注意:T 必须是继承自QProtobufMessage 并带有Q_PROTOBUF_OBJECT 宏的类,或者(对于嵌套的 Any 消息)必须是 Any 本身。
void Any::setTypeUrl(const QString &typeUrl)
将所含数据的类型 URL 设置为typeUrl 。
注意:不会 对输入进行验证,以检查其是否正确或格式是否有效。
注意: 这是属性typeUrl 的设置 函数。
另请参见 typeUrl() 和fromMessage()。
void Any::setValue(const QByteArray &value)
将存储在value 中的值的原始字节设置为该值。
value 必须是序列化消息后的输出结果。
注意: 这是属性value 的设置 函数。
另请参阅 value() 和fromMessage()。
QString Any::typeUrl() const
返回 Any 对象的类型 URL。该 URL 旨在提示所包含数据的真实类型。
注意:Qt 不支持动态获取任何可能用于拆解类型的规则,而是直接使用类型 URL 来验证作为 T 传递给 `as<T>() ` 的类型是否正确。
注意: 属性 typeUrl 的获取 函数。
另请参阅 setTypeUrl() 和value()。
template <typename T> std::optional<T> Any::unpack(QAbstractProtobufSerializer *serializer) const
该函数在使用serializer 对数据进行反序列化之前,会将T的消息名称与typeUrl()的返回值进行比较。
如果验证或反序列化失败,则返回std::nullopt 。
注意:T 必须是继承自QProtobufMessage 且包含Q_PROTOBUF_OBJECT 宏的类,或者(对于嵌套的 Any 消息)必须是 Any 本身。
QByteArray Any::value() const
返回构成所存储值的原始字节。
请参阅typeUrl() 以确定如何解释这些字节。
注意: 这是属性 value 的获取 函数。
[noexcept default] QtProtobuf::Any &Any::operator=(QtProtobuf::Any &&other)
将 `other ` 移动并赋值给此 `Any ` 实例。
QtProtobuf::Any &Any::operator=(const QtProtobuf::Any &other)
将other 中的数据复制到此实例中。
相关非成员
[noexcept] bool operator!=(const QtProtobuf::Any &lhs, const QtProtobuf::Any &rhs)
如果两个 Any 实例(lhs 和rhs )不相同,则返回 true。
[noexcept] bool operator==(const QtProtobuf::Any &lhs, const QtProtobuf::Any &rhs)
如果两个 `Any` 实例(lhs 和rhs )比较结果相等,则返回 true。
© 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.