From d75e772e2252a37c55ada3d84a301d3d296c1d02 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. Pick-to: 6.5 Task-number: QTBUG-111228 Change-Id: I3642e24e0d10721e4a0325b35a94dcb5dfbcd4e6 Reviewed-by: MÃ¥rten Nordheim --- .../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 40ff07863af..8a82991775e 100644 --- a/examples/corelib/serialization/rsslisting/rsslisting.cpp +++ b/examples/corelib/serialization/rsslisting/rsslisting.cpp @@ -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(); - } } /*