Qt Protobuf 変更可能なゲッター

生成された Qt Protobufメッセージは mutable ゲッターを使ってメッセージ・タイプのフィールドにアクセスできます。ゲッターはmut というプレフィックスを持ち、フィールドへの nononst リファレンスを返します。

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() &;
    ...
}

startend フィールドに対して、qtprotobufgenジェネレーターは追加の変更可能なゲッターを作成します:mutStartmutEnd 。これらのゲッターを使用して、中間メッセージを作成せずにフィールドを直接変更します:

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()<< ")";

mutableゲッターを呼び出すと、必要なフィールドの割り当てが行われ、基礎となるデータを直接変更することができる。

警告 Mutable ゲッターはフィールド名にmut というプレフィックスを付けます。メッセージにfieldmutField というフィールドがある場合、名前の衝突が起こります。このシナリオは現在サポートされておらず、ジェネレーター・エラーになります。

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