18

I am using Qt 4.6.3 and the following not-working code

QStringList userInfo;
QNetworkRequest netRequest(QUrl("http://api.stackoverflow.com/1.1/users/587532"));
QNetworkReply *netReply = netman->get(netRequest);

// from here onwards not working
netReply->waitForReadyRead(-1);
if (netReply->isFinished()==true)
{userInfo << do sth to reply;}
return userInfo;

as this function returns an empty QStringList, the app crashes. How to wait until the request has finished and then process the reply within one function

1
  • 1
    Old question, but still very relevant. Qt honestly should have a waitForFinished()' function for QNetworkReply.
    – LorenDB
    Commented Jul 15, 2021 at 13:39

3 Answers 3

46

You can use event loop:

QEventLoop loop;
connect(netReply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
// here you have done.

Also you should consider adding some shorter then network timeout (20s?). I'm not sure if finished is called even if an error occured. So it is possible, that you have connect to error signal also.

4
  • Also the other answer also includes mention of QEventLoop, this answer was correct at once. Commented Feb 14, 2012 at 9:24
  • 1
    havent come across any situation where the finished slot wasent called. i know that the doc states that it usally gets called but for me it always gets called
    – chikuba
    Commented May 10, 2012 at 0:06
  • It only means that you should consider connecting to some additional signals. It doesn't matter if it "always get called for me". There may be situation (as the docs say) when you won't get finished() signal, so your event loop will be stuck Commented May 10, 2012 at 11:30
  • Note that QApplication::quit() will leave the loop without having a finished reply. Commented Nov 28 at 16:53
6

These replies here are all using old syntax and do not apply to the latest QT.

To wait for the network request to finish:

QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
5

First I recommend you to read the relevant documentation from the Qt Documentation Reference that you can find here: http://doc.qt.nokia.com/latest/classes.html.

Looking at your code sample it seems that you already have, along side with QNetworkRequest and QNetworkReply, a QNetworkAccessManager. What you need is to connect a slot to the finished(QNetworkReply *) signal. This signal is emitted whenever a pending network reply is finished.

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
        this, SLOT(replyFinished(QNetworkReply*)));

manager->get(QNetworkRequest(QUrl("http://api.stackoverflow.com")));

Now, in your slot, you can read the data which was sent in response to your request. Something like:

void MyClass::MySlot(QNetworkReply *data) {
    QFile file("dataFromRequest");
    if (!file.open(QIODevice::WriteOnly))
        return;
    file.write(data->readAll());
    file.close();
}

EDIT:

To wait synchronously for a signal use QEventLoop. You have an example here

http://wiki.forum.nokia.com/index.php/How_to_wait_synchronously_for_a_Signal_in_Qt

1
  • I also know this but how would I be able to return that received data within same function (I dont\cant want\have an extra slot function)
    – yolo
    Commented Mar 30, 2011 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.