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);// 显示线条数据qDebug().nospace() << "start: (" << line.start().x() << "," << line.start().y() << ") " "end: ("<<line.end().x()<< ","<<line.end().y()<< ")";
调用可变获取器可执行任何必要的字段分配,并允许您直接修改底层数据。
警告: 可变获取器会在字段名后添加mut
前缀。如果一条信息包含名为field
和mutField
的字段,就会发生命名冲突。目前不支持这种情况,会导致生成器错误。
© 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.