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.
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:
The last two readings are:
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:
The reading was in 1048576 bytes - takes between 2 and 10 msecs to write.