このページでは

Qt Protobuf Well-Known Types C++ Classes

Qt Protobuf の「Well-Known Types」モジュールは、「Well-Known Types」パッケージで利用可能ないくつかの型をサポートしています。詳細...

このモジュールは Qt 6.6 で導入されました。

クラス

QtProtobuf::Any

protobuf との作業を簡略化するヘルパークラス。任意の型

詳細な説明

QtProtobufWellKnownTypes には、以下のProtobuf 既知の型が含まれています:サポートされている型:

ProtobufWellKnownTypes ライブラリの使用

プロジェクトで既知の型を使用するには、インターフェースにに対応する Google の .proto ファイルを含める必要があります:

syntax = "proto3";
package somepackage;
import "google/protobuf/any.proto";
message Message {
    google.protobuf.Any payload = 1;
}

リストされている型を使用するには、CMakeLists.txtファイルに次の行を追加して、ProtobufWellKnownTypes ライブラリをリンクする必要があります:

target_link_libraries(YourTargetName PRIVATE Qt::ProtobufWellKnownTypes)

ProtobufWellKnownTypes への QML サポートの追加

ProtobufWellKnownTypes ライブラリは QML サポートを提供していません。今後のリリースでサポートが実装される予定ですが、現時点では、以下の手順に従ってQt Protobuf の既知の型を QML 互換の型に拡張することができます。例えば、Empty はRPC メソッドの入力や出力としてよく使用されます。以下を参照してください:

syntax = "proto3";
package qtgrpc.examples;
import "google/protobuf/empty.proto";
service ExampleService {
    rpc emptyMethod(google.protobuf.Empty) returns (google.protobuf.Empty) {}
}

QMLからemptyMethod を呼び出すには、プロジェクトのヘッダーファイル(.h )に以下のコードを追加して、google.protobuf.Empty 型を宣言します:

#include "google/protobuf/empty.qpb.h"

struct GoogleProtobufAPI
{
    Q_GADGET
    QML_FOREIGN(google::protobuf::Empty)
    QML_STRUCTURED_VALUE
    QML_VALUE_TYPE(protobufEmpty)
};

その後、QML内でprotobufEmpty 値型を使用できるようになります。以下の例を参照してください:

import qtgrpc.examples
import QtQuick
import QtGrpc

Item {
    id: root
    property protobufEmpty value: null

    ServiceClient {
        id: grpcClient
        channel: grpcChannel.channel
    }

    Item {
        Component.onCompleted: {
            grpcClient.emptyMethod(value, messageCallback, errorCallback, grpcCallOptions);
        }
    }
}

QML_STRUCTURED_VALUE マクロを使用すると、次のような構文も利用できます:

Component.onCompleted: {
    grpcClient.emptyMethod({}, messageCallback, errorCallback, grpcCallOptions);
}

「 QML_VALUE_TYPE 」および「QML_STRUCTURED_VALUE 」も参照してください 。

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