Qt Protobuf Getter mutabili
I messaggi generati Qt Protobuf consentono di accedere ai campi di un tipo di messaggio utilizzando getter mutabili. I getter hanno il prefisso mut e restituiscono un riferimento non costante al campo.
message Point {
double x = 1;
double y = 2;
}
message Line {
Point start = 1;
Point end = 2;
}Lo schema .proto sopra riportato genera il seguente codice per il messaggio Line:
class Line : public QProtobufMessage
{
const Point &start() const &;
Point &mutStart() &;
...
const Point &end() const &;
Point &mutEnd() &;
...
}Per i campi start e end, il generatore qtprotobufgen crea ulteriori getter modificabili: mutStart e mutEnd. Utilizza questi getter per modificare direttamente i campi, senza creare messaggi intermedi:
Line line;
// Impostazione del punto iniziale della linea su (5.0, 5.0)
line.mutStart().setX(5.0);
line.mutStart().setY(5.0);
// Impostazione del punto finale della linea in (10.0, 20.0)
line.mutEnd().setX(10.0);
line.mutEnd().setY(20.0);
// Visualizzazione dei dati della linea
qDebug().nospace() << "start: (" << line.start().x() << "," << line.start().y() << ") "
"end: (" << line.end().x() << "," << line.end().y() << ")";La chiamata ai getter mutabili esegue l'allocazione dei campi necessaria e consente di modificare direttamente i dati sottostanti.
Attenzione: i gettermutabili aggiungono il prefisso mut ai nomi dei campi. Se un messaggio contiene campi denominati field e mutField, si verifica un conflitto di nomi. Questo scenario non è attualmente supportato e causerà un errore del generatore.
© 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.