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.

Task-number: QTBUG-111228
Change-Id: I3642e24e0d10721e4a0325b35a94dcb5dfbcd4e6
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit d75e772e2252a37c55ada3d84a301d3d296c1d02)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Edward Welbourne 2023-05-05 11:16:12 +02:00 committed by Qt Cherry-pick Bot
parent cc2b50846a
commit 306cb0140d

View File

@ -76,15 +76,17 @@ RSSListing::RSSListing(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.
}
/*
@ -107,10 +109,7 @@ void RSSListing::fetch()
fetchButton->setEnabled(false);
treeWidget->clear();
xml.clear();
QUrl url(lineEdit->text());
get(url);
get(QUrl(lineEdit->text()));
}
void RSSListing::metaDataChanged()
@ -131,11 +130,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();
}
}
/*