PDA

View Full Version : instead of Qeventloop which method uset to wait for response



iswaryasenthilkumar
23rd April 2015, 15:41
here below code i used QEventloop for waiting for response,,instead of QEventloop is there any method to wait for response ,can any one give me suggestion for this:confused:
Thanks in advance:o


QNetworkAccessManager manager;
QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url)));
QEventLoop event;
connect(response,SIGNAL(finished()),&event,SLOT(quit()));
event.exec();
QString html = response->readAll();

yeye_olive
23rd April 2015, 15:56
That is the way to go if you want to wait for the reply to finish, although I am not sure if the finished() signal is always guaranteed to be emitted in case of error.

Unless this synchronous approach greatly simplifies the design of your application, I suggest you follow the asynchronous approach and partition your code into the part sending the request and the part handling the reply. E.g.


void MyObject::sendRequest(/* ... */) {
// Send the request
manager.get(QNetworkRequest(QUrl(url)));
connect(&manager, SIGNAL(finished(QNetworkReply *)), SLOT(handleReply(QNetworkReply *)));
}

void MyObject::handleReply(QNetworkReply *reply) {
// Handle the reply here
QString html = reply->readAll();
// ...
}

Last but not least, you have not connected to readyRead(), which means that the reply may never emit finished() if it is large, and your event loop will run forever.

iswaryasenthilkumar
24th April 2015, 08:36
i need to wait for response,,i used your code but i got networkstatus=0,,while using eventloop my statuscode getting proper result.i should wait for response instead of using qeventloop.please help me
That is the way to go if you want to wait for the reply to finish, although I am not sure if the finished() signal is always guaranteed to be emitted in case of error.

Unless this synchronous approach greatly simplifies the design of your application, I suggest you follow the asynchronous approach and partition your code into the part sending the request and the part handling the reply. E.g.


void MyObject::sendRequest(/* ... */) {
// Send the request
manager.get(QNetworkRequest(QUrl(url)));
connect(&manager, SIGNAL(finished(QNetworkReply *)), SLOT(handleReply(QNetworkReply *)));
}

void MyObject::handleReply(QNetworkReply *reply) {
// Handle the reply here
QString html = reply->readAll();
// ...
}

Last but not least, you have not connected to readyRead(), which means that the reply may never emit finished() if it is large, and your event loop will run forever.

wysota
24th April 2015, 10:21
What is wrong with using QEventLoop?

anda_skoa
26th April 2015, 10:26
i need to wait for response,,i used your code but i got networkstatus=0

Where do you get that?

Cheers,
_

iswaryasenthilkumar
27th April 2015, 14:27
while using QEventloop it wait for until i got response no other event will execute,,thats the problem,Thats why i asked is there any method to waiting process instead of QEventloop?

wysota
27th April 2015, 15:10
while using QEventloop it wait for until i got response no other event will execute
That's not true.

iswaryasenthilkumar
28th April 2015, 05:59
i havee doubt in this code can you explain please


QUrl url1(pic);
QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url1)));
QEventLoop event;
connect(response,SIGNAL(finished()),&event,SLOT(quit()));//this eventloop will quit untill it reaches finished,,if suppose we not get response from mangaer what will happen here,,event will still in waiting process to quit am i correct.
event.exec();


That's not true.

anda_skoa
28th April 2015, 06:54
Why are you going back to using an event loop after yeye_olive explained how you can do it without, the very question that is still the subject of this thread?

And yes, an event loop continues its execution until its quit() method is called.

Cheers,
_