以下代码来自一个示例,展示了如何使用QNetworkAccessManager下载内容。
void Downloader::replyFinished (QNetworkReply *reply)
{
if(reply->error())
{
qDebug() << "ERROR!";
qDebug() << reply->errorString();
}
else
{
qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
QFile *file = new QFile("C:/Qt/Dummy/downloaded.txt");
if(file->open(QFile::Append))
{
file->write(reply->readAll());
file->flush();
file->close();
}
delete file;
}
reply->deleteLater();
}
我的问题是我们必须致电回复 - > gt; deleteLater();这里?如果我们不调用它,当我们第二次执行QNetworkAccessManager :: get()调用时,插槽中的QNetworkReply *是否与QNetworkReply *相同?
答案 0 :(得分:1)
如果不调用deleteLater(),QNetworkReply对象将被泄露,其内存不会被释放。第二个get()调用将创建一个新的QNetworkReply对象。