When using QNetworkreply::readAll
to write data to a QFile
, at the time the download arrives at the end, there is a fast freeze of 2 to 4 seconds and it also varies depending on the network or site I am downloading, to normal and the download ends.
It seems that the buffer read increases more than normal at the end
Here is an example code:
int main(int argc, char** argv)
{
QCoreApplication a(argc, argv);
QFile file("C:/exemplo/bigfile.7z");
if(!file.open(QIODevice::WriteOnly)) {
return 1;
}
QNetworkAccessManager manager;
QNetworkRequest request(QUrl("http://localhost/bigfile.7z"));
QNetworkReply *reply = manager.get(request);
QObject::connect(reply, &QNetworkReply::readyRead, [&](){
QTime time;
time.start();
QByteArray ba = reply->readAll();
file.write(ba);
int duration= time.elapsed();
qDebug() << "read" << ba.size() << " bytes of data. (" << duration << " msecs)";
});
QObject::connect(reply, &QNetworkReply::finished, [&](){
qDebug() << "finished!";
a.quit();
});
return a.exec();
}
This problem occurs when I try to download large files like 300mb for example, see the result:
Note that at the end of the download (practically 99%), reply->readAll
and write
, takes 1617msecs and then 2741msecs, or almost 4 seconds, see that readAll
on the last two calls returned much more data .
This causes a small crash in GUI applications (which use QWidget
).