QProtobufSerializer Class
The QProtobufSerializer class is interface that represents basic functions for serialization/deserialization. More...
| Header: | #include <QProtobufSerializer> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Protobuf)target_link_libraries(mytarget PRIVATE Qt6::Protobuf) |
| Since: | Qt 6.5 |
| Inherits: | QAbstractProtobufSerializer |
Public Functions
| QProtobufSerializer() | |
| virtual | ~QProtobufSerializer() override |
(since 6.13) quint64 | maximumInputSize() const |
(since 6.13) void | setMaximumInputSize(quint64 size) |
(since 6.7) void | shouldPreserveUnknownFields(bool preserveUnknownFields) |
Reimplemented Public Functions
| virtual QAbstractProtobufSerializer::Error | lastError() const override |
| virtual QString | lastErrorString() const override |
Detailed Description
The QProtobufSerializer class registers serializers/deserializers for classes implementing a protobuf message, inheriting QProtobufMessage. These classes are generated automatically, based on a .proto file, using the CMake function qt_add_protobuf or by running qtprotobufgen directly.
Message size limits
A serialized protobuf message must be smaller than 2 GiB. This is the biggest size that all protobuf implementations support. So QProtobufSerializer does not serialize a message of 2 GiB or more, and does not deserialize input data of 2 GiB or more. When a size limit is exceeded, lastError() returns QAbstractProtobufSerializer::Error::MessageTooLarge.
- Serialization limit: serialize() fails if the serialized message is 2 GiB or larger.
- Deserialization limit: deserialize() fails, without parsing, if the input data is larger than the maximum input size. By default, it is the same as the serialization limit. You can make it smaller with setMaximumInputSize(), or with the
QT_PROTOBUF_MAXIMUM_INPUT_SIZEenvironment variable. It does not affect serialize().
The two limits are separate, like in other protobuf implementations such as C++ and Java. The 2 GiB serialization limit is a rule of the protobuf format, so it is fixed. A smaller serialization limit would not help, because your application creates the message itself, and serialize() checks the size only after the message is built in memory. The deserialization limit protects against input that may come from an untrusted source and can use a lot of memory. Only your application knows which size is reasonable, so you can set it.
The environment variable is used only if you do not call setMaximumInputSize(). It is read once per process, the first time the limit is needed. If you change it later, the new value is not used.
Note: Set a smaller deserialization limit for data that comes from untrusted sources. See maximumInputSize() for details.
Member Function Documentation
QProtobufSerializer::QProtobufSerializer()
Constructs a new serializer instance.
[override virtual noexcept] QProtobufSerializer::~QProtobufSerializer()
Destroys the serializer instance.
[override virtual] QAbstractProtobufSerializer::Error QProtobufSerializer::lastError() const
Reimplements: QAbstractProtobufSerializer::lastError() const.
Returns the last deserialization error for the serializer instance.
See also lastErrorString().
[override virtual] QString QProtobufSerializer::lastErrorString() const
Reimplements: QAbstractProtobufSerializer::lastErrorString() const.
Returns the last deserialization error string for the serializer instance.
See also lastError().
[since 6.13] quint64 QProtobufSerializer::maximumInputSize() const
Returns the maximum size, in bytes, that deserialize() will accept.
If the input data is larger than this value, deserialize() returns false without parsing, and lastError() returns QAbstractProtobufSerializer::Error::MessageTooLarge. This value does not affect serialize().
The value is chosen in this order:
- The size set with setMaximumInputSize().
- The
QT_PROTOBUF_MAXIMUM_INPUT_SIZEenvironment variable, if it is set to a non-negative integer. It is read once per process, the first time the limit is needed. Later changes to it are not used. - The default:
2 GiB - 1.
A value of 0 allows only empty input data. Values larger than 2 GiB - 1 are reduced to that value.
This function was introduced in Qt 6.13.
See also setMaximumInputSize().
[since 6.13] void QProtobufSerializer::setMaximumInputSize(quint64 size)
Sets the maximum size, in bytes, that deserialize() will accept to size. This size is used instead of the QT_PROTOBUF_MAXIMUM_INPUT_SIZE environment variable. It does not affect serialize().
A size of 0 allows only empty input data. Values larger than 2 GiB - 1 are reduced to that value.
It is recommended to bound the size of messages received from untrusted sources.
This function was introduced in Qt 6.13.
See also maximumInputSize().
[since 6.7] void QProtobufSerializer::shouldPreserveUnknownFields(bool preserveUnknownFields)
Controls whether the unknown fields received from the wire should be stored in the resulting message or if it should be omitted, based on preserveUnknownFields.
This function was introduced in Qt 6.7.
© 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.