我有以下代码:
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,而该过程仍然在后台下载文件。
我做错了什么?
答案 0 :(得分:1)
10秒可能不足以全部下载,具体取决于您的互联网连接。不要像这样使用计时器。要查看即时效果,请删除以下行:
int timeInSeconds = 10;
QTimer *idleTimer = new QTimer(this);
connect(idleTimer,SIGNAL(timeout()),&loop,SLOT(quit()));
idleTimer->setInterval(timeInSeconds *1000);
idleTimer->start();