QgsNetworkAccessManager class

network access manager for QGIS

It's a singleton that can be used across QGIS.

Plugins can insert proxy factories and thereby redirect requests to individual proxies.

If no proxy factories are there or none returns a proxy for an URL a fallback proxy can be set. There's also a exclude list that defines URLs that the fallback proxy should not be used for, then no proxy will be used.

Public static functions

static auto cacheLoadControlFromName(const QString& name) -> QNetworkRequest::CacheLoadControl
Gets QNetworkRequest::CacheLoadControl from name.
static auto cacheLoadControlName(QNetworkRequest::CacheLoadControl control) -> QString
Gets name for QNetworkRequest::CacheLoadControl.
static auto instance(Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection) -> QgsNetworkAccessManager*
Returns a pointer to the active QgsNetworkAccessManager for the current thread.

Public functions

auto excludeList() const -> QStringList
retrieve exclude list (urls shouldn't use the fallback proxy)
auto fallbackProxy() const -> const QNetworkProxy&
retrieve fall back proxy (for urls that no factory returned proxies for)
void insertProxyFactory(QNetworkProxyFactory* factory)
insert a factory into the proxy factories list
auto proxyFactories() const -> const QList<QNetworkProxyFactory*>
retrieve proxy factory list
void removeProxyFactory(QNetworkProxyFactory* factory)
remove a factory from the proxy factories list
void setFallbackProxyAndExcludes(const QNetworkProxy& proxy, const QStringList& excludes)
Sets fallback proxy and URL that shouldn't use it.
void setupDefaultProxyAndCache(Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection)
Setup the QgsNetworkAccessManager (NAM) according to the user's settings.
auto useSystemProxy() const -> bool
Returns whether the system proxy should be used.

Function documentation

static QgsNetworkAccessManager* QgsNetworkAccessManager::instance(Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection)

Returns a pointer to the active QgsNetworkAccessManager for the current thread.

Parameters
connectionType In most cases the default of using a Qt::BlockingQueuedConnection is ok, to make a background thread wait for the main thread to answer such a request is fine and anything else is dangerous. However, in case the request was started on the main thread, one should execute a local event loop in a helper thread and freeze the main thread for the duration of the download. In this case, if an authentication request is sent from the background thread network access manager, the background thread should be blocked, the main thread be woken up, processEvents() executed once, the main thread frozen again and the background thread continued.

With the connectionType parameter it is possible to setup the default connection type that is used to handle signals that might require user interaction and therefore need to be handled on the main thread. See in-depth discussion below.

void QgsNetworkAccessManager::setupDefaultProxyAndCache(Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection)

Setup the QgsNetworkAccessManager (NAM) according to the user's settings.

The connectionType sets up the default connection type that is used to handle signals that might require user interaction and therefore need to be handled on the main thread. See in-depth discussion in the documentation for the constructor of this class.