我有以下代码:

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>");

如果远程文件为5 MB,则可以正常工作;如果远程文件为50或500MB,则正常工作;程序仅保存前22 MB,而进程仍在后台下载文件。

我究竟做错了什么?

最佳答案

10秒可能不足以全部下载,具体取决于您的Internet连接。请勿使用这种计时器。要查看即时效果,请删除以下几行:

int timeInSeconds = 10;
QTimer *idleTimer = new QTimer(this);
connect(idleTimer,SIGNAL(timeout()),&loop,SLOT(quit()));
idleTimer->setInterval(timeInSeconds *1000);
idleTimer->start();

关于c++ - QFile.write()仅写入前22MBytes的大文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22001245/

10-11 16:25