Search code examples
qtqwidgetqtnetwork

Download big files with QNetworkreply::readAll freeze for a few seconds


I used example like doc http://doc.qt.io/qt-4.8/qnetworkaccessmanager.html

I create a startDownload:

connect(pushButton, SIGNAL(clicked(bool)), this, SLOT(startDownload(bool)));

In startDownload(bool) I put this:

file = new QFile("C:/foo/bar/bigfile.7z");
file->open(QIODevice::WriteOnly);

QNetworkRequest request;
request.setUrl(QUrl("http://localhost/bigfile.7z"));
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");

QNetworkReply *reply = manager->get(request);

connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
        this, SLOT(slotError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
        this, SLOT(slotSslErrors(QList<QSslError>)));

In slotReadyRead I put this:

file->write(reply->readAll());

But when the download arrives at the end there is a small freezing 2 seconds and then returns to normal and the download is complete. This problem only occurs if the file I'm trying to transfer is large.


Solution

  • After tried use @Mike code, I noticed that the data read in readyRead final are much higher than the previous which makes it slow to write in file:

    without setReadBufferSize

    The last two readings are:

    1. 46080000 bytes - takes ~1.6 seconds to write.
    2. 227323951 bytes - takes ~2.7 seconds to write.

    It varies according to the network type and speed, allowing the buffer to be great or not.

    In GUI applications cause a sense of "freeze" for ~4 secs.

    For limite buffer I used QNetworkReply::setReadBufferSize, see the difference:

    with setReadBufferSize

    The reading was in 1048576 bytes - takes between 2 and 10 msecs to write.