Multiples Connections in QML
-
Hi, I'm trying to receive two jsonArrays from two different endpoints to build a list in QML with a Listview.
The json contains the same data structure, so I save the data in a single C++ object. In order to do that, I request the json from the first endpoint and then wait for the signal replyStatusChanged() to call a javascript controller that saves the json's content in the C++ object, and then repeat the process for the second one.
When I do this I encountered that the jsons arrives in any order an I need to wait for the first data to be fully loaded in the object before the writing of the second set of data starts.How can I check if the data of the second endpoint has been received and the data of the first endpoint has been written?. I was thinking on using two signals but I don't know how to do it.
The classes
Class a : public QObject { ... Q_INVOKABLE void get (); Q_INVOKABLE QJsonArray getJsonArray(); signals: void replyStatusChanged(); } Class b : public QObject { //Contains the data }
the main fucntion
int main(int argc, char *argv[]) { a endpoint1; a endpoint2; b container; engine.rootContext()->setContextProperty("endpoint1", &endpoint1); engine.rootContext()->setContextProperty("endpoint2", &endpoint2); engine.rootContext()->setContextProperty("container", &container); ... return 0; }
In the QML file
Connections { target: endpoint1 onReplyStatusChanged: controller.setData1() } Connections { target: endpoint2 onReplyStatusChanged: controller.setData2() }
-
Not quite understand your question.. Generally speaking, Javascript application use a Promise object for deferred and asynchronous operation. For example , do something when two or more asynchronous operations completed (in any order).
Qt do not bundle any Promise implementation by default. You could get a one from my github repo:
Example
Promise { resolveWhen: Q.all([endpoint1. onReplyStatusChanged,endpoint2. onReplyStatusChanged]); onFulfilled: { // It will be triggered only if both of the endpoint emitted the signal controller.setData1(); controller.setData2(); } }