In questa pagina

Qt Protobuf Well-Known Types C++ Classes

Il modulo " Qt Protobuf " (Tipi ben noti) di offre il supporto per alcuni dei tipi disponibili nel pacchetto " Well-Known Types ". Maggiori informazioni...

Questo modulo è stato introdotto in Qt 6.6.

Classi

QtProtobuf::Any

Una classe di supporto per semplificare l'utilizzo del tipo Any di protobuf

Descrizione dettagliata

QtProtobufWellKnownTypes contiene i seguenti tipi Protobuf ben noti: Tipi supportati:

Utilizzo della libreria ProtobufWellKnownTypes

Per utilizzare i tipi ben noti nel proprio progetto, è possibile includere il corrispondente file .proto di Google nell'interfaccia:

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

Per utilizzare i tipi elencati, è necessario collegarsi alla libreria ProtobufWellKnownTypes aggiungendo la seguente riga al file CMakeLists.txt:

target_link_libraries(YourTargetName PRIVATE Qt::ProtobufWellKnownTypes)

Aggiunta del supporto QML per ProtobufWellKnownTypes

La libreria ProtobufWellKnownTypes non fornisce il supporto QML. Sebbene tale supporto verrà implementato nelle versioni future, per ora è possibile estendere i tipi ben noti di Qt Protobuf in tipi compatibili con QML seguendo le istruzioni riportate di seguito. Ad esempio, Empty viene spesso utilizzato come input o output per i metodi RPC. Vedi sotto:

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

Per richiamare emptyMethod da QML, dichiarare il tipo google.protobuf.Empty aggiungendo il codice seguente a un file di intestazione ( .h ) del progetto:

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

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

Successivamente, è possibile utilizzare il tipo di valore ` protobufEmpty ` in QML. Vedi l’esempio qui sotto:

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 consente anche questa sintassi:

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

Vedi anche 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.