NetworkAccessManagerFactory: Complete documentation page with snippets

Pick-to: 6.5 6.5.0
Fixes: QTBUG-110657
Change-Id: I063eb4ac25cbe226b5a996014042515d6ada5468
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Olivier De Cannière 2023-02-10 16:05:54 +01:00
parent ea8c9a0dad
commit d023d149d9
12 changed files with 41 additions and 7 deletions

View File

@ -12,6 +12,4 @@ if(TARGET Qt::Quick)
endif()
qt_internal_add_example(dynamicscene)
endif()
if(QT_FEATURE_qml_network AND TARGET Qt::Quick)
qt_internal_add_example(networkaccessmanagerfactory)
endif()

View File

@ -5,9 +5,6 @@ qtHaveModule(quick) {
SUBDIRS += \
qmlextensionplugins \
xmlhttprequest
qtConfig(qml-network): \
SUBDIRS += networkaccessmanagerfactory
}
SUBDIRS += \

View File

@ -0,0 +1,24 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
//! [0]
class CachingNetworkAccessManagerFactory : public QQmlNetworkAccessManagerFactory
{
public:
inline QNetworkAccessManager *create(QObject *parent) override
{
QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(parent);
QNetworkDiskCache *diskCache = new QNetworkDiskCache(parent);
diskCache->setCacheDirectory("requestCache");
networkAccessManager->setCache(diskCache);
return networkAccessManager;
}
};
//! [0]
//! [1]
CachingNetworkAccessManagerFactory networkManagerFactory;
engine->setNetworkAccessManagerFactory(&networkManagerFactory);
//! [1]

View File

@ -18,9 +18,20 @@ QT_BEGIN_NAMESPACE
with custom QNetworkAccessManager instances with specialized caching,
proxy and cookies support.
\list
\li The QNetworkDiskCache can be used as a request cache with \l {QNetworkDiskCache}.
\li Using \l {QNetworkProxy}, traffic sent by the QNetworkAccessManager can be tunnelled through a proxy.
\li Cookies can be saved for future requests by adding a \l {QNetworkCookieJar}.
\endlist
To implement a factory, subclass QQmlNetworkAccessManagerFactory and
implement the virtual create() method, then assign it to the relevant QML
engine using QQmlEngine::setNetworkAccessManagerFactory().
engine using QQmlEngine::setNetworkAccessManagerFactory(). For instance, the QNetworkAccessManager
objects created by the following snippet will cache requests.
\snippet code/src_network_access_qnetworkaccessmanager.cpp 0
The factory can then be passed to the QML engine so it can instantiate the QNetworkAccessManager with the custom behavior.
\snippet code/src_network_access_qnetworkaccessmanager.cpp 1
Note the QML engine may create QNetworkAccessManager instances
from multiple threads. Because of this, the implementation of the create()
@ -45,6 +56,7 @@ QT_BEGIN_NAMESPACE
\l {Threads and QObjects} and \l {Signals and Slots Across Threads}.
\sa {C++ Extensions: Network Access Manager Factory Example}{Network Access Manager Factory Example}
\sa QNetworkDiskCache
*/
/*!

View File

@ -17,3 +17,6 @@ add_subdirectory(touch)
add_subdirectory(quickcontrols)
add_subdirectory(quickdialogs)
add_subdirectory(frameanimation)
if(QT_FEATURE_qml_network AND TARGET Qt::Quick)
qt_internal_add_example(networkaccessmanagerfactory)
endif()