Qt Protobuf Obtenedores mutables

Los mensajes Qt Protobuf permiten acceder a los campos de un tipo de mensaje mediante getters mutables. Los getters tienen el prefijo mut y devuelven una referencia no-const al campo.

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

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

El esquema .proto anterior genera el siguiente código para el mensaje Line:

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

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

Para los campos start y end, el generador qtprotobufgen crea getters mutables adicionales: mutStart y mutEnd. Utiliza estos getters para modificar los campos directamente, sin crear mensajes intermedios:

Line line;// Establecer el punto inicial de la línea en (5.0, 5.0) line.mutStart().setX(5.0); line.mutStart().setY(5.0);// Establecer el punto final de la línea en (10.0, 20.0)line.mutEnd().setX(10.0); line.mutEnd().setY(20.0);// Mostrar los datos de la línea.qDebug().nospace() << "start: (" << line.start().x() << "," << line.start().y() << ") "
   "end: ("<< line.end().x()<< ","<< line.end().y()<< ")";

La llamada a los getters mutables realiza cualquier asignación de campos necesaria y permite modificar los datos subyacentes directamente.

Advertencia: Los getters mutables añaden un prefijo mut a los nombres de campo. Si un mensaje contiene campos denominados field y mutField, se produce un conflicto de nombres. Este escenario no está soportado actualmente y resultará en un error del generador.

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