Reputation: 1284
I have a program which is supposed to download files over the net. And everything is fine as long as files are small'ish(<1GB). But when files are larger than 1GB one of two things happen:
a) if I connect signal from QNetworkReply with download progress to one of my slots, application crashes with std::bad_alloc
b) if I don't connect any signal from QNetworkReply, application stops on 73%.
Any help mostly appreciated.
Here is the code I use in my connection, and slot:
QNetworkReply* reply = network_access_manager_->get(request);
connect(reply,SIGNAL(downloadProgress(qint64,qint64)),parent_,SLOT(downloadProgress(qint64,qint64)));
And here is the slot:
void MainWindow::downloadProgress(qint64 bytesReceived,qint64 bytesTotal)
{
try
{
ui->label->setText(QString::number(bytesReceived));
ui->label_2->setText(QString::number(bytesTotal));
ui->progressBar->setRange(0,bytesTotal);
ui->progressBar->setValue(bytesReceived);
}
catch(std::exception& e)
{
qDebug() << e.what();
}
}
Upvotes: 1
Views: 1719
Reputation: 9996
Read the documentation of QNetworkReply:
QNetworkReply is a sequential-access QIODevice, which means that once data is read from the object, it no longer kept by the device. It is therefore the application's responsibility to keep this data if it needs to. Whenever more data is received from the network and processed, the readyRead() signal is emitted.
Therefore, just read from QNetworkReply during download, whenever you want, using the readyRead() signal, and write to file or wherever you want.
Here you can find some other information (read Frank Osterfeld's answer in particular): Downloading File in Qt From URL.
Upvotes: 3