Qt Protobuf Métodos de lectura mutables
Los Qt Protobuf permiten acceder a los campos de un tipo de mensaje mediante getter mutables. Los getter llevan el prefijo « mut » y devuelven una referencia no constante al campo.
message Point {
double x = 1;
double y = 2;
}
message Line {
Point start = 1;
Point end = 2;
}El esquema anterior « .proto » 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 métodos de lectura mutables adicionales: mutStart y mutEnd. Utiliza estos métodos de lectura 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() << ")";Al llamar a los getter mutables se lleva a cabo cualquier asignación de campos necesaria y se permite modificar directamente los datos subyacentes.
Advertencia: Los gettermutables añaden el prefijo « mut » a los nombres de los campos. Si un mensaje contiene campos denominados « field » y « mutField », se produce un conflicto de nombres. Esta situación no está soportada actualmente y dará lugar a 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.