RSS listing example: prefer setDevice() over addData(readAll())
The network reply is a QIODevice, so the QXmlStreamReader can be set to read from it directly, which it might potentially do incrementally, rather than by reading all the data in one go. In the process, change the set-up of the reply to first check that it got a valid URL, then check it got a reply, before doing things with that reply. Pick-to: 6.5 Task-number: QTBUG-111228 Change-Id: I3642e24e0d10721e4a0325b35a94dcb5dfbcd4e6 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
parent
0c5135a9df
commit
d75e772e22
@ -69,15 +69,17 @@ RSSListing::RSSListing(const QString &url, QWidget *parent)
|
||||
*/
|
||||
void RSSListing::get(const QUrl &url)
|
||||
{
|
||||
QNetworkRequest request(url);
|
||||
if (currentReply) {
|
||||
currentReply->disconnect(this);
|
||||
currentReply->deleteLater();
|
||||
}
|
||||
currentReply = manager.get(request);
|
||||
connect(currentReply, &QNetworkReply::readyRead, this, &RSSListing::readyRead);
|
||||
connect(currentReply, &QNetworkReply::metaDataChanged, this, &RSSListing::metaDataChanged);
|
||||
connect(currentReply, &QNetworkReply::errorOccurred, this, &RSSListing::error);
|
||||
currentReply = url.isValid() ? manager.get(QNetworkRequest(url)) : nullptr;
|
||||
if (currentReply) {
|
||||
connect(currentReply, &QNetworkReply::readyRead, this, &RSSListing::readyRead);
|
||||
connect(currentReply, &QNetworkReply::metaDataChanged, this, &RSSListing::metaDataChanged);
|
||||
connect(currentReply, &QNetworkReply::errorOccurred, this, &RSSListing::error);
|
||||
}
|
||||
xml.setDevice(currentReply); // Equivalent to clear() if currentReply is null.
|
||||
}
|
||||
|
||||
/*
|
||||
@ -100,10 +102,7 @@ void RSSListing::fetch()
|
||||
fetchButton->setEnabled(false);
|
||||
treeWidget->clear();
|
||||
|
||||
xml.clear();
|
||||
|
||||
QUrl url(lineEdit->text());
|
||||
get(url);
|
||||
get(QUrl(lineEdit->text()));
|
||||
}
|
||||
|
||||
void RSSListing::metaDataChanged()
|
||||
@ -124,11 +123,8 @@ void RSSListing::metaDataChanged()
|
||||
void RSSListing::readyRead()
|
||||
{
|
||||
int statusCode = currentReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (statusCode >= 200 && statusCode < 300) {
|
||||
QByteArray data = currentReply->readAll();
|
||||
xml.addData(data);
|
||||
if (statusCode >= 200 && statusCode < 300)
|
||||
parseXml();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
Loading…
x
Reference in New Issue
Block a user