QML Receive signal once for each element
-
Hi. I have a problem with receiving signals. I need to get only one signal for element, when it initialize, but I've got for each element too much signals with almost same value. My code looks like this:
CalendarView.qml
.... Item { .... property bool visibility: false; Component.onCompleted: { eventModel.eventsCountForDate(styleData.date); eventModel.eventsCount.connect(setVisible); } function setVisible(result) { visibility = result > 0; } Image { visible: visibility .... source: "qrc:/assets/event.png" } .... }
Item's parent element has property for date, called styleData.date.
My functions, which calls GET request from server:void EventModel::replyCountFunction(QNetworkReply* reply) { //Test for errors, and get reply data emit eventsCount(response.getCount()); return; } void EventModel::eventsCountForDate(const QDate &date) { //Work with date and request QNetworkAccessManager* manager = new QNetworkAccessManager(); QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyCountFunction(QNetworkReply*)), Qt::UniqueConnection); manager->get(request); }
When I debug this, I've got that onComplete QML parameter calls correct, which calling function eventCountForDate, which calls correct too, for my calendar it's 42 times, for each date in visible part of calendar. And function replyCountFunction, also works 42 times, but for each time, it sends back to QML function setVisible for 42 times per one calling reply function. My log looks like this:
..... Calling function replyCountFunction qml: Connected 0 qml: Connected 0 ..... //another 39 times qml: Connected 0 Calling function replyCountFunction qml: Connected 0 qml: Connected 0 .....
Where 0 is count of events for day. Sometimes it will send me 42 "one's" (1) for one reply, it happens too.
I think, reason in connect function, maybe I need to disconnect function for manager, but I create a custom manager in each calling of eventsCountForDate. I had almost the same problem, but for single date, when I search events for selected date, and it fixed by separating NetworkAccessManager for each request (cause I've used one network manager for two requests - get list of events for date in one request, and get event parameters in another request). But here it's not working.
Maybe you know, how can I organize this work. Thank you.