Rectangle 27 0

qt QNetworkReply wait for finished?


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.

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

You can use event loop:

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

Note
Rectangle 27 0

qt QNetworkReply wait for finished?


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

manager->get(QNetworkRequest(QUrl("http://api.stackoverflow.com")));
void MyClass::MySlot(QNetworkReply *data) {
    QFile file("dataFromRequest");
    if (!file.open(QIODevice::WriteOnly))
        return;
    file.write(data->readAll());
    file.close();
}

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.

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)

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.

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

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

Note