From 306cb0140dc266e7f57347084461f191c0138a8f Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 5 May 2023 11:16:12 +0200 Subject: [PATCH] RSS listing example: prefer setDevice() over addData(readAll()) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 (cherry picked from commit d75e772e2252a37c55ada3d84a301d3d296c1d02) Reviewed-by: Qt Cherry-pick Bot --- .../serialization/rsslisting/rsslisting.cpp | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/examples/corelib/serialization/rsslisting/rsslisting.cpp b/examples/corelib/serialization/rsslisting/rsslisting.cpp index 9269be4080c..7ce35c9f2a3 100644 --- a/examples/corelib/serialization/rsslisting/rsslisting.cpp +++ b/examples/corelib/serialization/rsslisting/rsslisting.cpp @@ -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(); - } } /*