Qt Protobuf Well-Known Types C++ Classes
El módulo Qt Protobuf Well-Known Types proporciona soporte para algunos de los tipos disponibles en el paquete Well-Known Types. Más...
Este módulo se introdujo en Qt 6.6.
Clases
Una clase de ayuda para simplificar el trabajo con el protobuf Cualquier tipo |
Descripción detallada
QtProtobufWellKnownTypes contiene los siguientes tipos Protobuf Well-Known: Tipos soportados:
- Any (mensaje)
- Api (mensaje)
- BoolValue (mensaje)
- BytesValue (mensaje)
- DoubleValue (mensaje)
- Duration (mensaje)
- Empty (mensaje)
- Enum (mensaje)
- EnumValue (mensaje)
- Field (mensaje)
- Field.Cardinality (enum)
- Field.Kind (enum)
- FieldMask (mensaje)
- FloatValue (mensaje)
- Int32Value (mensaje)
- Int64Value (mensaje)
- ListValue (mensaje)
- Method (mensaje)
- Mixin (mensaje)
- NullValue (enum)
- Option (mensaje)
- SourceContext (mensaje)
- StringValue (mensaje)
- Struct (mensaje)
- Sintaxis (enum)
- Timestamp (mensaje)
- Type (mensaje)
- UInt32Value (mensaje)
- UInt64Value (mensaje)
- Valor (mensaje)
Uso de la biblioteca ProtobufWellKnownTypes
Para utilizar tipos bien conocidos en su proyecto puede incluir el correspondiente archivo google.proto en su interfaz:
syntax = "proto3";
package somepackage;
import "google/protobuf/any.proto";
message Message {
google.protobuf.Any payload = 1;
}Para utilizar los tipos listados debes enlazar con la librería ProtobufWellKnownTypes añadiendo la siguiente línea a tu fichero CMakeLists.txt:
target_link_libraries(YourTargetName PRIVATE Qt::ProtobufWellKnownTypes)
Añadir soporte QML para ProtobufWellKnownTypes
La librería ProtobufWellKnownTypes no proporciona soporte QML. Aunque el soporte se implementará en futuras versiones, por ahora puedes extender los tipos bien conocidos de Qt Protobuf a tipos compatibles con QML usando las siguientes instrucciones. Por ejemplo, Empty se utiliza a menudo como entrada o salida de métodos RPC. Véase más abajo:
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, declare 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, puede 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.