Qt Protobuf Mutable Getters

The generated Qt Protobuf messages allow access to the fields of a message type using mutable getters. The getters have the mut prefix and return a non-const reference to the field.

message Point {
    double x = 1;
    double y = 2;
}

message Line {
    Point start = 1;
    Point end = 2;
}

The above .proto scheme generates the following code for the Line message:

class Line : public QProtobufMessage
{
    const Point &start() const &;
    Point &mutStart() &;
    ...

    const Point &end() const &;
    Point &mutEnd() &;
    ...
}

For the start and end fields, the qtprotobufgen generator creates additional mutable getters: mutStart and mutEnd. Use these getters to modify fields directly, without creating intermediate messages:

Line line;

// Setting the line start point to (5.0, 5.0)
line.mutStart().setX(5.0);
line.mutStart().setY(5.0);

// Setting the line end point to (10.0, 20.0)
line.mutEnd().setX(10.0);
line.mutEnd().setY(20.0);

// Display the Line data
qDebug().nospace() << "start: (" << line.start().x() << "," << line.start().y() << ") "
    "end: (" << line.end().x() << "," << line.end().y() << ")";

Calling the mutable getters performs any necessary field allocation and allows you to modify the underlying data directly.

Warning: Mutable getters add a mut prefix to field names. If a message contains fields named field and mutField, a naming conflict occurs. This scenario is currently unsupported and will result in a generator error.

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