diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2023-05-05 11:16:12 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2023-05-16 20:09:38 +0200 |
commit | d75e772e2252a37c55ada3d84a301d3d296c1d02 (patch) | |
tree | 51cf4937ef614127df9bc5ddbe1d78446fe4e0c6 | |
parent | 0c5135a9dfa6140d23d86b001c3054891c22dcb9 (diff) | |
download | qtbase-d75e772e2252a37c55ada3d84a301d3d296c1d02.tar.gz |
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>
-rw-r--r-- | examples/corelib/serialization/rsslisting/rsslisting.cpp | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/examples/corelib/serialization/rsslisting/rsslisting.cpp b/examples/corelib/serialization/rsslisting/rsslisting.cpp index 40ff07863a..8a82991775 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(); - } } /* |