Qt Protobuf Well-Known Types C++ Classes | Qt Protobuf | Qt 6.12.0
Description: The Qt Protobuf Well-Known Types module provides support for some of the types available from the Well-Known Types package.
Keywords: module

- Qt 6.12
- Qt Protobuf
- Qt Protobuf Well-Known Types C++ Classes
- Qt 6.12.0 Reference Documentation

# Qt Protobuf Well-Known Types C++ Classes

The Qt Protobuf Well-Known Types module provides support for some of the types available from the Well-Known Types package. More...

This module was introduced in Qt 6.6.

## Classes

QtProtobuf::Any | A helper class to simplify working with the protobuf Any type

## Detailed Description

QtProtobufWellKnownTypes contains the following Protobuf Well-Known types: Supported types:

- Any (message)
- Api (message)
- BoolValue (message)
- BytesValue (message)
- DoubleValue (message)
- Duration (message)
- Empty (message)
- Enum (message)
- EnumValue (message)
- Field (message)
- Field.Cardinality (enum)
- Field.Kind (enum)
- FieldMask (message)
- FloatValue (message)
- Int32Value (message)
- Int64Value (message)
- ListValue (message)
- Method (message)
- Mixin (message)
- NullValue (enum)
- Option (message)
- SourceContext (message)
- StringValue (message)
- Struct (message)
- Syntax (enum)
- Timestamp (message)
- Type (message)
- UInt32Value (message)
- UInt64Value (message)
- Value (message)

### Using the ProtobufWellKnownTypes library

To use well-known type in your project you may include the corresponding google .proto file in your interface:

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

To use the types listed you must link with the `ProtobufWellKnownTypes` library by adding the following line to your CMakeLists.txt file:

     target_link_libraries(YourTargetName PRIVATE Qt::ProtobufWellKnownTypes)

### Adding QML support for ProtobufWellKnownTypes

The ProtobufWellKnownTypes library doesn't provide the QML support. Although the support will be implemented in future releases, for now you can extend the Qt Protobuf well-known types into QML-compatible types using the following instructions. For example, Empty is often used as an input or output for RPC methods. See below:

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

To call `emptyMethod` from QML, declare the `google.protobuf.Empty` type by adding the following code to a header file `.h` file of the project:

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

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

After, you can then use the `protobufEmpty` value type in QML. See the example below:

     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);
             }
         }
     }

The QML_STRUCTURED_VALUE macro also enables this syntax:

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

See also QML_VALUE_TYPE and QML_STRUCTURED_VALUE.