QProtobufMessage Class | Qt Protobuf | Qt 6.11.1
Description: Base class for all protobuf messages.

- Qt 6.11
- Qt Protobuf
- C++ Classes
- QProtobufMessage
- Qt 6.11.1 Reference Documentation

# QProtobufMessage Class

Base class for all protobuf messages. More...

Header: | `#include <QProtobufMessage>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Protobuf)` `target_link_libraries(mytarget PRIVATE Qt6::Protobuf)`
Since: | Qt 6.5
Inherited By: | QtProtobuf::Any

- List of all members, including inherited members

## Public Functions

`(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

## Static Public Members

QProtobufMessagePointer | constructByName(const QString &messageType)

## Related Non-Members

| QProtobufMessagePointer
`(since 6.11)` To | qprotobufmessage_cast(QProtobufMessage *from)
`(since 6.11)` To | qprotobufmessage_cast(const QProtobufMessage *from)

## Detailed Description

Provides access to the properties of a message, using setProperty() and property(), without depending on what the message is.

## Member Function Documentation

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

Constructs QProtobufMessage using messageType. Returns a pointer to the constructed QProtobufMessage.

This function attempts to create a message with a type that matches messageType. If messageType is unknown, the function returns `nullptr`. If the message is not found in the registry, the function returns `nullptr`. The function caller is given ownership of the constructed message.

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

Deserializes this protobuf message from a QByteArray data using serializer. Returns `true` if deserialization was successful, otherwise `false`.

This function was introduced in Qt 6.8.

See also serialize().

### QVariant QProtobufMessage::property(QAnyStringView propertyName) const

Get the value of the property propertyName.

If the propertyName isn't known then the returned QVariant is invalid.

See also setProperty().

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

Returns the pointer to the property ordering of the derived protobuf message.

This function was introduced in Qt 6.8.

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

Serializes this protobuf message into a QByteArray using serializer.

This function was introduced in Qt 6.8.

See also deserialize().

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

Set the property propertyName to the value stored in value.

If the propertyName isn't a known fields, then the value is ignored, and the function returns `false`.

Returns `false` if it fails to store the value on the property. Otherwise `true`.

See also property().

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

This is an overloaded function.

This function was introduced in Qt 6.6.

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

Returns the unknown field values sorted as they were received from the wire.

This function was introduced in Qt 6.7.

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

Returns the field numbers that were not known to QtProtobuf during deserialization.

This function was introduced in Qt 6.7.

## Related Non-Members

### `[alias]` QProtobufMessagePointer

Synonym for std::unique_ptr<QProtobufMessage, QProtobufMessageDeleter>. Use this to manage the lifetime of dynamically allocated QProtobufMessages, such as those created by calling 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)

Returns the given from object cast to pointer type `To` if the object is of type `To` (or a subclass); otherwise returns `nullptr`. If from is `nullptr`, this function also returns `nullptr`.

The template argument `To` must be a pointer to a class that inherits from QProtobufMessage and is declared with the Q_PROTOBUF_OBJECT macro.

Example:

     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

This function behaves similarly to the standard C++ `dynamic_cast()`, with the advantages that it doesn't require RTTI support and works across dynamic library boundaries.

Warning: If the type pointed to by `To` is not declared with the Q_PROTOBUF_OBJECT macro, this function's return value is undefined.

These functions were introduced in Qt 6.11.