Nesta página

Qt Protobuf Well-Known Types C++ Classes

O módulo “ Qt Protobuf: Well-Known Types” oferece suporte a alguns dos tipos disponíveis no pacote “Well-Known Types ”. Mais...

Este módulo foi introduzido no Qt 6.6.

Classes

QtProtobuf::Any

Uma classe auxiliar para simplificar o trabalho com o tipo `Any` do protobuf

Descrição detalhada

O QtProtobufWellKnownTypes contém os seguintes tipos bem conhecidos do Protobuf: Tipos suportados:

Usando a biblioteca ProtobufWellKnownTypes

Para utilizar tipos bem conhecidos em seu projeto, você pode incluir o arquivo .proto correspondente do Google em sua interface:

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

Para utilizar os tipos listados, você deve vincular à biblioteca ProtobufWellKnownTypes adicionando a seguinte linha ao seu arquivo CMakeLists.txt:

target_link_libraries(YourTargetName PRIVATE Qt::ProtobufWellKnownTypes)

Adicionando suporte a QML para ProtobufWellKnownTypes

A biblioteca ProtobufWellKnownTypes não oferece suporte a QML. Embora esse suporte venha a ser implementado em versões futuras, por enquanto você pode estender os tipos bem conhecidos do Qt Protobuf para tipos compatíveis com QML seguindo as instruções a seguir. Por exemplo, o tipo `Empty` é frequentemente usado como entrada ou saída para métodos RPC. Veja abaixo:

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

Para chamar o emptyMethod a partir do QML, declare o tipo google.protobuf.Empty adicionando o código a seguir a um arquivo de cabeçalho ( .h ) do projeto:

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

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

Depois disso, você poderá usar o tipo de valor ` protobufEmpty ` no QML. Veja o exemplo abaixo:

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

A macro ` QML_STRUCTURED_VALUE ` também permite esta sintaxe:

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

Veja também QML_VALUE_TYPE e 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.