diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/network/doc/snippets/code/src_network_kernel_qnetworkinformation.cpp | 49 | ||||
| -rw-r--r-- | src/network/kernel/qnetworkinformation.cpp | 17 |
2 files changed, 65 insertions, 1 deletions
diff --git a/src/network/doc/snippets/code/src_network_kernel_qnetworkinformation.cpp b/src/network/doc/snippets/code/src_network_kernel_qnetworkinformation.cpp new file mode 100644 index 00000000000..a68579d0740 --- /dev/null +++ b/src/network/doc/snippets/code/src_network_kernel_qnetworkinformation.cpp @@ -0,0 +1,49 @@ +// Copyright (C) 2025 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//![file] +#include <QCoreApplication> +#include <QNetworkInformation> +#include <QDebug> + +void onReachabilityChanged(QNetworkInformation::Reachability reachability) { + switch (reachability) { + case QNetworkInformation::Reachability::Unknown: + qDebug() << "Network reachability is unknown."; + break; + case QNetworkInformation::Reachability::Disconnected: + qDebug() << "Network is disconnected."; + break; + case QNetworkInformation::Reachability::Local: + qDebug() << "Network is locally reachable."; + break; + case QNetworkInformation::Reachability::Site: + qDebug() << "Network can reach the site."; + break; + case QNetworkInformation::Reachability::Online: + qDebug() << "Network is online."; + break; + } +} + +int main(int argc, char *argv[]) { + QCoreApplication a(argc, argv); + + // Check if QNetworkInformation is supported + if (!QNetworkInformation::loadDefaultBackend()) { + qWarning() << "QNetworkInformation is not supported on this platform or backend."; + return 1; + } + + QNetworkInformation* netInfo = QNetworkInformation::instance(); + + // Connect to the reachabilityChanged signal + QObject::connect(netInfo, &QNetworkInformation::reachabilityChanged, + &onReachabilityChanged); + + // Print initial status + onReachabilityChanged(netInfo->reachability()); + + return a.exec(); +} +//![file] diff --git a/src/network/kernel/qnetworkinformation.cpp b/src/network/kernel/qnetworkinformation.cpp index d3e27f5a552..fc113e45393 100644 --- a/src/network/kernel/qnetworkinformation.cpp +++ b/src/network/kernel/qnetworkinformation.cpp @@ -415,6 +415,20 @@ QNetworkInformationBackendFactory::~QNetworkInformationBackendFactory() Various plugins can have various functionality supported, and so you can load() plugins based on which features are needed. + In most cases, the recommended approach is to load the + platform-specific backend by calling loadDefaultBackend(). This will + automatically select the most appropriate backend available on the + current platform and is suitable for the majority of applications. + + \snippet code/src_network_kernel_qnetworkinformation.cpp file + + For more advanced uses cases, developers may prefer to load a backend + based on specific capabilities or preferences. loadBackendByFeatures() + allows selecting a backend that supports a specific set of features, + such as reporting the transport mediom or signal strength. Alternatively, + loadBackendByName() allows loading a plugin by its name, which can include + platform-specific or custom backend implementations. + QNetworkInformation is a singleton and stays alive from the first successful load() until destruction of the QCoreApplication object. If you destroy and re-create the QCoreApplication object you must call @@ -765,7 +779,8 @@ QStringList QNetworkInformation::availableBackends() /*! Returns a pointer to the instance of the QNetworkInformation, - if any. + if any. If this method is called before a backend is loaded, + it returns a null pointer. \sa load() */ |
