車両
Qt gRPC™クライアントと C++gRPC サーバ間の2つのスレッド接続を管理する。
この例では、gRPC サーバから送信されたデータを表示する車両ダッシュボードをシミュレートしています。
サンプルコードには以下のコンポーネントがあります:
vehicle_client
qt_add_protobuf()およびqt_add_grpc()CMake 関数を使用してメッセージとサービスを生成する Qt クライアントアプリケーション。gRPCvehicle_server
C++ プラグインを呼び出してサーバーコードを生成し、簡単なサーバーロジックを実装するアプリケーション。gRPC
注意: C++gRPC プラグインがインストールされている必要があります。詳細はこちらをご覧ください:モジュールの前提条件
どちらのコンポーネントも、vehicleservice.proto
およびnavigationservice.proto
に記述されている protobuf スキーマから生成されたメッセージを使用します。
車両サービス:
message SpeedMsg { int32 speed = 1; } message RpmMsg { int32 rpm = 1; } message AutonomyMsg { int32 autonomy = 1; } service VehicleService { rpc getSpeedStream(google.protobuf.Empty) returns (stream SpeedMsg) {} rpc getRpmStream(google.protobuf.Empty) returns (stream RpmMsg) {} rpc getAutonomy(google.protobuf.Empty) returns (AutonomyMsg) {} }
ナビゲーションサービス:
enum DirectionEnum { RIGHT = 0; LEFT = 1; STRAIGHT = 2; BACKWARD = 3; } message NavigationMsg { int32 totalDistance = 1; int32 traveledDistance = 2; DirectionEnum direction = 3; string street = 4; } service NavigationService { rpc getNavigationStream(google.protobuf.Empty) returns (stream NavigationMsg) {} }
VehicleManager
C++のシングルトンは、2つのQThread インスタンスを使い、並行してサーバーと通信する。スレッドには、gRPC 異なる接続タイプがある。この例では、2つのタイプがある:
- サーバー・ストリーミングRPC 例えば、車両スレッドの速度ストリーム。QGrpcServerStream::messageReceived 2つのコールバック関数を使用しています。QGrpcOperation::finished
空のspeedRequest; m_streamSpeed= m_client->getSpeedStream(speedRequest); connect(m_streamSpeed.get(), &:messageReceived, this)QGrpcServerStream::messageReceived, this, [this]() {if(const autospeedResponse= m_streamSpeed->read<SpeedMsg>()) {emitspeedChanged(speedResponse->speed()); } }); connect( m_streamSpeed.get(), &::finished,this,[this]()QGrpcServerStream::finished, this,[this](constQGrpcStatus&status) {if(!status.isOk()) {autoerror=QString("Stream error fetching speed %1 (%2)").arg(status.message()).arg(QVariant::fromValue(status.code()).toString()); connectionError(error)を出力します; qWarning() << error; return; }}、 Qt::SingleShotConnection);
- 単項 RPC RPC
getAutonomy
操作は単項 RPC である。これは単一の応答を返す。これはQGrpcOperation::finished シグナルにのみ接続される。空の autonomyRequest; std::unique_ptr<QGrpcCallReply> autonomyReply= m_client->getAutonomy(autonomyRequest);const auto *autonomyReplyPtr =autonomyReply.get(); connect( autonomyReplyPtr, &::finished,this, [thisQGrpcCallReply::finished, this,[this,autonomyReply=std::move(autonomyReply)](constQGrpcStatus&status) {if(!status.isOk()) {autoerror=QString("Call error fetching autonomy %1 (%2)").arg(status.message()).arg(QVariant::fromValue(status.code()).toString()); connectionError(error) を返します; qWarning() << error; return; }if(const autoautonomyMsg= autonomyReply->read<AutonomyMsg>()) {emitautonomyChanged(autonomyMsg->autonomy()); }}、 Qt::SingleShotConnection);
クライアントメインウィンドウのインターフェースは Main.qml ファイルで定義される。これは、VehicleManager
C++シングルトンのシグナルをカスタムスロットに接続するために、QMLのConnections 型を使用しています:
Connections { target: VehicleManager // This slot will be executed when the VehicleManager::totalDistanceChanged // signal is emitted function onTotalDistanceChanged(distance: int): void { root.totalDistance = distance; } function onSpeedChanged(speed: int): void { root.speed = speed; } function onRpmChanged(rpm: int): void { root.rpm = rpm; } function onTraveledDistanceChanged(distance: int): void { root.traveledDistance = distance; } function onDirectionChanged(direction: int): void { if (direction == VehicleManager.RIGHT) { root.directionImageSource = "qrc:/direction_right.svg"; } else if (direction == VehicleManager.LEFT) { root.directionImageSource = "qrc:/direction_left.svg"; } else if (direction == VehicleManager.STRAIGHT) { root.directionImageSource = "qrc:/direction_straight.svg"; } else { root.directionImageSource = ""; } }
レスポンスを受信すると、クライアントウィンドウは受信したデータでUIを更新します。このようにして、異なるスレッドでメッセージを受信し、シグナルのおかげでスレッドセーフな方法でクライアントUIに送信することができます。
© 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.