1

我有以下代码:

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 或 500 MB,程序只保存前 22 MB,而进程仍然在后台下载文件。

我究竟做错了什么?

4

1 回答 1

1

10 秒可能不足以下载全部内容,具体取决于您的互联网连接。不要像这样使用计时器。要查看直接效果,请删除以下行:

int timeInSeconds = 10;
QTimer *idleTimer = new QTimer(this);
connect(idleTimer,SIGNAL(timeout()),&loop,SLOT(quit()));
idleTimer->setInterval(timeInSeconds *1000);
idleTimer->start();
于 2014-02-24T23:23:23.647 回答