QNetworkDiskCache: Update doc snippets

We should encourage users to use the system cache directory.
We should also encourage users to not use the char* QString-ctors.

QStandardPaths::CacheLocation is documented to never be empty, so use it
in this manner.

Pick-to: 6.5
Change-Id: I1f050feda5b0a8e7c23fbed5db904bb0180b45f0
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Mårten Nordheim 2023-03-10 18:22:54 +01:00
parent 58dfb10781
commit a156b667ce

View File

@ -4,17 +4,20 @@
//! [0] //! [0]
QNetworkAccessManager *manager = new QNetworkAccessManager(this); QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkDiskCache *diskCache = new QNetworkDiskCache(this); QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
diskCache->setCacheDirectory("cacheDir"); QString directory = QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
+ QLatin1StringView("/cacheDir/");
diskCache->setCacheDirectory(directory);
manager->setCache(diskCache); manager->setCache(diskCache);
//! [0] //! [0]
//! [1] //! [1]
using namespace Qt::StringLiterals;
// do a normal request (preferred from network, as this is the default) // do a normal request (preferred from network, as this is the default)
QNetworkRequest request(QUrl(QString("http://qt-project.org"))); QNetworkRequest request(QUrl(u"http://qt-project.org"_s));
manager->get(request); manager->get(request);
// do a request preferred from cache // do a request preferred from cache
QNetworkRequest request2(QUrl(QString("http://qt-project.org"))); QNetworkRequest request2(QUrl(u"http://qt-project.org"_s));
request2.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); request2.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
manager->get(request2); manager->get(request2);
//! [1] //! [1]