Is it possible to use protobuf/gRPC generated types in QML?
-
I'm exited about QT's new gRPC support. I'm using it in my current project nextapp, an open source personal manager / productivity app.
It's works perfectly, and is much simpler to use asynchronously than the native gRPC generated C++ client code. Wrapping it in a trivial C++ template; calling RPC on the server boils down to simply this:
nextapp::pb::GetNodesReq req; callRpc<nextapp::pb::NodeTree>([this](nextapp::pb::GetNodesReq req) { return client_->GetNodes(req); }, [this](const nextapp::pb::NodeTree& tree) { emit receivedNodeTree(tree); }, req);Also, the generated types are using native QT types like QString, so they can be used directly in C++ without any type conversion. Very well done!
Now to my question. I'm using QT 6.6.1. I noticed that there is generated code to use the protobuf generated classes in QML. However, I can not figure out how to get QML to recognize them.
For example - I may have an object representing a person. I want to pass it to QML to populate a Dialog. Then, when the user press "Save", I want to update the data in the object, and send it back to C++ so that the new data can be sent to the server.
What am I missing? Note that I'm only trying to use the generated protobuf data types. For now I want C++ to handle all server communication.
The QT code is here
-
I'm exited about QT's new gRPC support. I'm using it in my current project nextapp, an open source personal manager / productivity app.
It's works perfectly, and is much simpler to use asynchronously than the native gRPC generated C++ client code. Wrapping it in a trivial C++ template; calling RPC on the server boils down to simply this:
nextapp::pb::GetNodesReq req; callRpc<nextapp::pb::NodeTree>([this](nextapp::pb::GetNodesReq req) { return client_->GetNodes(req); }, [this](const nextapp::pb::NodeTree& tree) { emit receivedNodeTree(tree); }, req);Also, the generated types are using native QT types like QString, so they can be used directly in C++ without any type conversion. Very well done!
Now to my question. I'm using QT 6.6.1. I noticed that there is generated code to use the protobuf generated classes in QML. However, I can not figure out how to get QML to recognize them.
For example - I may have an object representing a person. I want to pass it to QML to populate a Dialog. Then, when the user press "Save", I want to update the data in the object, and send it back to C++ so that the new data can be sent to the server.
What am I missing? Note that I'm only trying to use the generated protobuf data types. For now I want C++ to handle all server communication.
The QT code is here
@jarle protobuf messages are registered in QML as valuetypes, so the answer to your question 'yes' in general. I will jump into your project this week(probably this evening, who knows) and will give the answer :) The module is in TP we expect that there might be issues for sure :(
-
@jarle protobuf messages are registered in QML as valuetypes, so the answer to your question 'yes' in general. I will jump into your project this week(probably this evening, who knows) and will give the answer :) The module is in TP we expect that there might be issues for sure :(
-
@semlanik Great! I'm looking forward to get this working ;)
I added a section on the projects readme page about how to run the backend in docker.
-
@jarle I submitted a pull request the the code snippet. Also I will add the documentation topic that describes how to use protobuf types in QML later. And thanks, using your APP I identified couple major issues in current code.
Thanks a lot @semlanik !
I'm seeing one annoying problem with QT 6.7.0-beta2
When I compile under Ubuntu, I get this message:
In file included from /var/local/build/NextAppUi-Desktop_Qt_6_7_0_64-Debug/nextapp_client.grpc.qpb.cpp:3: /var/local/build/NextAppUi-Desktop_Qt_6_7_0_64-Debug/nextapp_client.grpc.qpb.h:21:1: error: declaration of anonymous class must be a definition 21 | class QPB_NEXTAPPGRPCCLIENT_EXPORT Client : public QAbstractGrpcClientWhich seems to boil down do
QPB_NEXTAPPGRPCCLIENT_EXPORTnot being defined on my system.My work-around for now was to just add
#ifndef QPB_NEXTAPPGRPCCLIENT_EXPORT # define QPB_NEXTAPPGRPCCLIENT_EXPORT #endifto
Qt/6.7.0/gcc_64/include/QtGrpc/qabstractgrpcclient.h -
Thanks a lot @semlanik !
I'm seeing one annoying problem with QT 6.7.0-beta2
When I compile under Ubuntu, I get this message:
In file included from /var/local/build/NextAppUi-Desktop_Qt_6_7_0_64-Debug/nextapp_client.grpc.qpb.cpp:3: /var/local/build/NextAppUi-Desktop_Qt_6_7_0_64-Debug/nextapp_client.grpc.qpb.h:21:1: error: declaration of anonymous class must be a definition 21 | class QPB_NEXTAPPGRPCCLIENT_EXPORT Client : public QAbstractGrpcClientWhich seems to boil down do
QPB_NEXTAPPGRPCCLIENT_EXPORTnot being defined on my system.My work-around for now was to just add
#ifndef QPB_NEXTAPPGRPCCLIENT_EXPORT # define QPB_NEXTAPPGRPCCLIENT_EXPORT #endifto
Qt/6.7.0/gcc_64/include/QtGrpc/qabstractgrpcclient.h@jarle yeah that's something I noticed too, see:
https://codereview.qt-project.org/c/qt/qtgrpc/+/537001/6
https://codereview.qt-project.org/c/qt/qtgrpc/+/537004/3