Nyaruko
Nyaruko

Reputation: 4499

QNetworkAccessManager: how to make sure download is finished?

If I do:

QNetworkRequest newRequest(url);
newRequest.setUrl(url);
QNetworkReply *reply = networkManager->get(newRequest);
connect(reply, SIGNAL(finished()), this, SLOT(onRetrievedDownLoadInfo()));

and

void myClass::onRetrievedDownLoadInfo()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(QObject::sender());

    if (!reply)
    {
        ui->upgradeLog->appendPlainText("Download failed, invalid context");
        return;
    }

    //Remeber to delete the reply
    //---------------------------
    reply->deleteLater();

    if (reply->error() != QNetworkReply::NoError)
    {
        ui->upgradeLog->appendPlainText("Download failed, invalid context");
        return;
    }

    reply->readAll();
    ...
}

Can I say that when the program reaches line

reply->readAll();

All the content replied by the server is successfully received?

Is it possible the reply->readAll(); only returns part of the reply due to network failure?

Upvotes: 0

Views: 298

Answers (1)

xProxamer
xProxamer

Reputation: 36

The error is here:

connect(reply, SIGNAL(finished()), this, SLOT(onRetrievedDownLoadInfo()));

you are not doing it in the correct way. Use a QNetworkAccessManager to check the network is working:

QNetworkConfigurationManager manager;
req.setConfiguration(manager.defaultConfiguration());

connect(&req, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));

Upvotes: 2

Related Questions