センサー
「Sensors」のサンプルでは、2つのアプリケーションがUDPソケットを使用してProtobufメッセージを送信することで通信を行う方法を示しています。
「Sensors」サンプルは、以下のコンポーネントで構成されています:
protobuf_sensorsqt_add_protobuf呼び出しを使用して、Protobuf スキーマから生成するライブラリ。protobuf_sensor_emulator単純なセンサーの動作をエミュレートするアプリケーション。protobuf_sensors_clientUDPソケットからセンサーデータを表示するアプリケーション。
クライアントアプリケーションは、localhost のUDPポート65500 にバインドし、エミュレータアプリケーションからのデータを待機します。

エミュレータアプリケーションを使用して、センサーデータの値を変更し、そのデータをクライアントのUDPポートに送信します。

各アプリケーションは、protobuf_sensors ライブラリによって生成されたメッセージを使用して通信を行います。このライブラリは、.protoファイルに記述されたprotobufスキーマから生成されます。
qt_add_protobuf(protobuf_sensors
PROTO_FILES
sensors.proto
tlv.proto
)最初のファイルは、センサーデータをカプセル化する Type-Length-Value メッセージを記述しています:
package qt.examples.sensors.tlv;
enum MessageType {
Coordinates = 0;
Temperature = 1;
WarningNotification = 2;
}
// Protobuf messages imply inline data size.
message TlvMessage {
MessageType type = 1;
bytes value = 2;
}2つ目の.protoファイルには、センサーメッセージの記述が含まれています:
package qt.examples.sensors;
message Coordinates {
double longitude = 1;
double latitude = 2;
double altitude = 3;
}
message Temperature {
enum Unit {
Farenheit = 0;
Celsius = 1;
}
sint32 value = 1;
Unit units = 2;
}
message WarningNotification {
string text = 1;
}メッセージは、クライアント側でインスタンス化するQProtobufSerializer を使用してシリアル化されます:
class SensorClient : public QObject
{
Q_OBJECT
...
private:
QUdpSocket m_client;
QProtobufSerializer m_serializer;
};また、エミュレータでは:
class SensorEmulator : public QObject
{
Q_OBJECT
...
QUdpSocket m_socket;
QProtobufSerializer m_serializer;
};エミュレータアプリケーションで「Send 」ボタンをクリックすると、QLineEdit のフィールドにあるデータが、文字列形式からメッセージのフィールド固有の形式に変換されます。例えば、「Coordinates」メッセージのフィールドの場合はdouble型になります:
QObject::connect(ui->sendCoordinates, &QPushButton::clicked, this, [this]() {
qt::examples::sensors::Coordinates coord;
coord.setLatitude(ui->latitudeValue->text().toDouble());
coord.setLongitude(ui->longitudeValue->text().toDouble());
coord.setAltitude(ui->altitudeValue->text().toDouble());
emit coordinatesUpdated(coord);
});その後、すべてのフィールドを含むメッセージがシリアル化され、「Type-Length-Value」メッセージでラップされます:
Q_ASSERT(serializer != nullptr);
tlv::TlvMessage msg;
msg.setType(type);
msg.setValue(data);
return msg.serialize(serializer);クライアントは、受信したデータグラムに対して逆の処理を適用します。まず、データグラムのデータから「Type-Length-Value」メッセージがデシリアライズされます:
const autodatagram=m_client.receiveDatagram();
qt::examples::Qt Sensors::tlv::TlvMessage msg;
msg.deserialize(&m_serializer,datagram.data());
if(m_serializer.lastError()
!=QAbstractProtobufSerializer::Error::None) {
qWarning().nospace() << "Unable to deserialize datagram ("
<< qToUnderlying(m_serializer.lastError())<< ")"
<<m_serializer.lastErrorString();
continue;
}その後、Type-Length-Valueメッセージがデシリアライズされ、センサーメッセージに変換されます:
qt::examples::sensors::Coordinates coord;
coord.deserialize(&m_serializer, msg.value());
emit coordinatesUpdated(coord);
break;最後に、変換されてユーザーに表示されます:
ui->latitudeValue->setText(QString::number(coord.latitude(), 'f', 7));
ui->longitudeValue->setText(QString::number(coord.longitude(), 'f', 7));
ui->altitudeValue->setText(QString::number(coord.altitude(), 'f', 7));注: このサンプルを実行する前に 、お使いのオペレーティングシステムが UDP ポート65500 へのバインディングおよび UDP 経由でのデータ送信を許可していることを確認してください。
© 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.