【发布时间】:2016-10-13 05:45:33
【问题描述】:
我正在使用 Qt 从 HTTP 下载文件,为此,我使用了两个信号:
connect(currentDownload, SIGNAL(finished()),
SLOT(downloadFinished()));
connect(currentDownload, SIGNAL(readyRead()),
SLOT(downloadReadyRead()));
回调:
void DownloadManager::downloadFinished()
{
output.close();
if (currentDownload->error()) {
// download failed
std::cout << "Failed: [" << currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()
<< "] " << qPrintable(currentDownload->errorString()) << std::endl;
} else {
std::cout << "Download succeeded" << std::endl;
}
currentDownload->deleteLater();
startNextDownload();
}
void DownloadManager::downloadReadyRead()
{
output.write(currentDownload->readAll());
}
我遇到的问题是,如果在下载过程中由于某种原因出现任何错误,我无法弄清楚如何跳过写入输出文件。这不合逻辑——因为我只能在finished() 信号发射期间检查错误。在readyRead() 信号期间检查错误不会显示任何错误,即使它们存在。结果,404 输出文件将被写入磁盘。我该如何解决?
【问题讨论】:
标签: c++ qt c++11 networking