Qt Protobuf 変更可能なゲッター
生成された Qt Protobuf メッセージでは、可変ゲッターを使用してメッセージ型のフィールドにアクセスできます。これらのゲッターは「mut 」というプレフィックスを持ち、フィールドへの非const参照を返します。
message Point {
double x = 1;
double y = 2;
}
message Line {
Point start = 1;
Point end = 2;
}上記の「.proto 」スキーマでは、Line メッセージに対して以下のコードが生成されます:
class Line : public QProtobufMessage
{
const Point &start() const &;
Point &mutStart() &;
...
const Point &end() const &;
Point &mutEnd() &;
...
}start およびend フィールドに対して、qtprotobufgenジェネレータは、mutStart およびmutEnd という追加の可変ゲッターを生成します。これらゲッターを使用すると、中間メッセージを作成することなく、フィールドを直接変更できます:
Line line;
// 線の始点を (5.0, 5.0) に設定
line.mutStart().setX(5.0);
line.mutStart().setY(5.0);
// 線の終点を (10.0, 20.0) に設定
line.mutEnd().setX(10.0);
line.mutEnd().setY(20.0);
// Line データを表示
qDebug().nospace() << "start: (" << line.start().x() << "," << line.start().y() << ") "
"end: (" <<line.end().x()<< "," <<line.end().y()<< ")";可変のゲッターを呼び出すと、必要なフィールドの割り当てが行われ、基になるデータを直接変更できるようになります。
警告: 可変 ゲッターは、フィールド名に「mut 」というプレフィックスを追加します。メッセージに「field 」および「mutField 」という名前のフィールドが含まれている場合、命名上の競合が発生します。このシナリオは現在サポートされておらず、ジェネレータエラーが発生します。
© 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.