PDA

View Full Version : How to wait until the request has finished and then process the reply within one fn



vinayaka
14th December 2011, 10:00
hai,



void HttpPost::on_pushButton_clicked()
{
params.addQueryItem("testSample","indu");
postData = params.encodedQuery();
manager->setCookieJar(new QNetworkCookieJar(manager));
connect (manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinish(QNetworkReply*)));
manager->post(req, postData);

}






void HttpPost::replyFinish(QNetworkReply* reply)
{

QString answer = QString::fromUtf8(reply->readAll());
qDebug() << "answer------------>"<<answer;

}






This code is working for me but i don't want to use the "connect" here. I want the reply in the same function. I used
if (reply->isFinished()==true) but it doesnt work
How to wait until the request has finished and then process the reply within one function

Gokulnathvc
14th December 2011, 11:28
Catch finished() signal!!! which is being called after complete download

vinayaka
14th December 2011, 11:56
Can you please give some more details or code as i needed it now to add it to my project.:D

Gokulnathvc
14th December 2011, 13:28
You need to use isbytesavailable() signal, in order to know the status of the download. And then check for the finished() signal. it will be called once the download is completed.

stampede
14th December 2011, 16:55
This code is working for me but i don't want to use the "connect" here. I want the reply in the same function.
Why ? What's wrong with the connect() ?
Anyway, to answer your question, you will have to block the processing somehow. It's not tested, but maybe you can
1) busy waiting with processEvents calls:


void HttpPost::on_pushButton_clicked()

{
params.addQueryItem("testSample","indu");
postData = params.encodedQuery();
manager->setCookieJar(new QNetworkCookieJar(manager));
QNetworkReply * reply = manager->post(req, postData);
while( ! reply->isFinished() ){
qApp->processEvents();
}
// reply should be finished by now
}

2) use QDialog (or QProgressDialog)


void HttpPost::on_pushButton_clicked()

{
params.addQueryItem("testSample","indu");
postData = params.encodedQuery();
manager->setCookieJar(new QNetworkCookieJar(manager));
QDialog dialog; dialog.setWindowTitle("Waiting...");
// ... prepare the dialog layout somehow
connect (manager, SIGNAL(finished(QNetworkReply *)), &dialog, SLOT(accept()));
QNetworkReply * reply = manager->post(req, postData);
dialog.exec();
// reply should be finished by now, but not if you close the dialog manually
}

Again, this code is not tested.

ChrisW67
15th December 2011, 00:06
If you really must have a blocking behaviour then you can use QEventLoop, connect the reply finished() signal to the quit() slot of the loop, and call exec(). When loop.exec() returns you need to check for errors before assuming the request worked. You might also want to implement a timeout in case the request hangs for an extended period.

On another note... much of the setup you have in the on_pushButton_clicked() function probably should be elsewhere (constructor maybe). You probably do not want to create a new connection to a finished() slot and new cookie jar every time the button is pushed.