QFile.write() writes only the first 22MBytes of large files
Question
I have following code:
QNetworkAccessManager *nam = new QNetworkAccessManager(this);
QNetworkReply *re = nam->get(QNetworkRequest(QUrl("http://somesite/ai.bin")));
QEventLoop loop;
QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit()));
int timeInSeconds = 10;
QTimer *idleTimer = new QTimer(this);
connect(idleTimer,SIGNAL(timeout()),&loop,SLOT(quit()));
idleTimer->setInterval(timeInSeconds *1000);
idleTimer->start();
loop.exec();
//save
QFile file("C:/a.jpg");
file.open(QIODevice::WriteOnly);
file.write(re->readAll());
ui->dbgOut->insertHtml("<font color='green'>OK</font><br>");
If the remote file is 5 MB, it works, bt if the file is abot 50 or 500MB, the program saves only the first 22 MBytes, while the process still downloads the file in background.
What am I doing wrong?
Solution
10 seconds may not be enough to download it all, depending on your internet connection. Do not use the timer like this. To see the immediate effect, drop these lines:
int timeInSeconds = 10;
QTimer *idleTimer = new QTimer(this);
connect(idleTimer,SIGNAL(timeout()),&loop,SLOT(quit()));
idleTimer->setInterval(timeInSeconds *1000);
idleTimer->start();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow