En esta página

Qt Protobuf Well-Known Types C++ Classes

El módulo « Qt Protobuf » de tipos conocidos ofrece compatibilidad con algunos de los tipos disponibles en el paquete «Well-Known Types ». Más información...

Este módulo se introdujo en Qt 6.6.

Clases

QtProtobuf::Any

Una clase auxiliar para simplificar el trabajo con el tipo Any de Protobuf

Descripción detallada

QtProtobufWellKnownTypes contiene los siguientes tipos «Well-Known» de Protobuf: Tipos compatibles:

Uso de la biblioteca ProtobufWellKnownTypes

Para utilizar tipos bien conocidos en tu proyecto, puedes incluir el archivo .proto correspondiente de Google en tu interfaz:

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

Para utilizar los tipos enumerados, debes vincular la biblioteca « ProtobufWellKnownTypes » añadiendo la siguiente línea a tu archivo CMakeLists.txt:

target_link_libraries(YourTargetName PRIVATE Qt::ProtobufWellKnownTypes)

Incorporación de compatibilidad con QML para ProtobufWellKnownTypes

La biblioteca ProtobufWellKnownTypes no ofrece compatibilidad con QML. Aunque esta compatibilidad se implementará en futuras versiones, por ahora puedes ampliar los tipos conocidos de Qt Protobuf a tipos compatibles con QML siguiendo estas instrucciones. Por ejemplo, «Empty» se utiliza a menudo como entrada o salida para métodos RPC. Véase a continuación:

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

Para llamar a « emptyMethod » desde QML, declara el tipo « google.protobuf.Empty » añadiendo el siguiente código a un archivo de cabecera « .h » del proyecto:

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

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

A continuación, ya podrá utilizar el tipo de valor « protobufEmpty » en QML. Véase el ejemplo siguiente:

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

La macro QML_STRUCTURED_VALUE también permite esta sintaxis:

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

Véase también QML_VALUE_TYPE y 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.