This tutorial will show how to use QtProtobuf client with some existing server and specified communication protocol.
After you successfully installed/built QtProtobuf (see README.md) with complete set of dependencies you may start creation either CMake or qmake project in QtCreator. Now we need to tell our project about QtProtobuf. Add following line in your CMakeLists.txt to add QtProtobuf as project dependency:
At this point you will have full access to QtProtobuf libraries and macroses.
Let's imagine you have echo sever with following protocol definition saved to tutorial.proto:
Integration with your Qt application starts at point when you add protobuf and gRPC generation for .proto filed. Add qtprotobuf_generate function to your CMakeLists.txt as following:
We specified QML TRUE
to have generated classes visibled in QML. At this point we have direct access to all classes specified in tutorial.proto from C++ and QML contexts.
To use them in C++ you need to include tutorial.qpb.h to get all messages defined in tutorial.proto accessible in C++ context:
To access messages from QML code add appropriate import of protobuf package:
Now lets go deeper and try to interact with server using our gRPC API. For server communication we will have C++ model, that implements business logic and encapsulates QtGrpc client for communication purpose:
Here we specify our interface to communicate to QML part.
response
property provides recent response from server. We made it CONSTANT because pointer it selves don't need any modifications.request
invocable function expects pointer to EchoRequest, that will be utilized to call Echo remote procedure.m_client
keeps pointer to generated EchoServerClient.Definition part of EchoClientEngine includes two main points:
For gRPC channel we will use Http2 protocol without credentials. Simply create new channel with QGrpcInsecureChannelCredentials / QGrpcInsecureCallCredentials and pass it as parameter to attachChannel call right in constructor:
Request function is very simple only utilizes generated Echo method, one of possible implementations as below:
Let's stop at parameters, that we pass to Echo
method. First parameter is request. Methods generated by QtProtobuf pass only reference to method argument, that's why we derefence it and provide as value. Second parameter is pointer to response that will be modified when server response will be received. Important point here is that responce value passed as QPointer and its time of life is controlled by grpc client. In case if pointer was removed client will ignore server response silently. So from this perspective QtGrpc provides to you async response safety.
We complete our model implementation, and it's time to make it visible in QML context. Simply do it in main:
It's important to call qRegisterProtobufTypes
to register all QtProtobuf types including generated user types. In our case it's types generated from tutorial.proto.
Let's move to presentation part. Of course it's better to use QML when we describe our project UI:
After cut off all fancy QML stuff, only QtProtobuf related things are left. Its important to do not forget import of our freshly generated qtprotobuf.tutorial package. At this point we have access to any message inside qtprotobuf.tutorial package. So we create view model right in QML code by adding EchoRequest
object and bind message
property to messageInput text.
Only thing left is to call our request method. We pass request that already contains text, that we would like to send to server. Result will be received and written to echoEngine.response.message property. We bound it to appropriate text field, that will be updated "immediately".
examples/tutorial/tutorialserver
folder. Run "build_and_run.sh" script in examples/tutorial/tutorialserver
folder and enjoy echo functionality of your QtProtobuf EchoService client UI.Please provide feedback using this issue in project bug tracker.