Qt Protobuf Mutable Getters | Qt Protobuf | Qt 6.10.3
Description: The use of mutable getters in Qt Protobuf

- Qt 6.10
- Qt Protobuf
- Qt Protobuf Mutable Getters
- Qt 6.10.3 Reference Documentation

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