QNetworkInformation Class

QNetworkInformation exposes various network information through native backends. 更多...

头: #include <QNetworkInformation>
CMake: find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network
Since: Qt 6.1
继承: QObject

公共类型

enum class 特征 { Reachability, CaptivePortal, TransportMedium, Metered }
flags 特征
enum class Reachability { Unknown, Disconnected, Local, Site, Online }
(since 6.3) enum class TransportMedium { Unknown, Ethernet, Cellular, WiFi, Bluetooth }

特性

公共函数

QString backendName () const
bool isBehindCaptivePortal () const
bool isMetered () const
QNetworkInformation::Reachability reachability () const
(since 6.3) QNetworkInformation::Features supportedFeatures () const
bool supports (QNetworkInformation::Features features ) const
QNetworkInformation::TransportMedium transportMedium () const

信号

void isBehindCaptivePortalChanged (bool state )
void isMeteredChanged (bool isMetered )
void reachabilityChanged (QNetworkInformation::Reachability newReachability )
void transportMediumChanged (QNetworkInformation::TransportMedium current )

静态公共成员

QStringList availableBackends ()
QNetworkInformation * 实例 ()
(从 6.4 起) bool loadBackendByFeatures (QNetworkInformation::Features features )
(从 6.4 起) bool loadBackendByName (QStringView backend )
(since 6.3) bool loadDefaultBackend ()

详细描述

QNetworkInformation provides a cross-platform interface to network-related information through plugins.

Various plugins can have various functionality supported, and so you can load() plugins based on which features are needed.

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 load() again.

另请参阅 QNetworkInformation::Feature .

成员类型文档编制

enum class QNetworkInformation:: 特征
flags QNetworkInformation:: 特征

Lists all of the features that a plugin may currently support. This can be used in QNetworkInformation::load().

常量 描述
QNetworkInformation::Feature::Reachability 0x1 If the plugin supports this feature then the reachability property will provide useful results. Otherwise it will always return Reachability::Unknown 。另请参阅 QNetworkInformation::Reachability .
QNetworkInformation::Feature::CaptivePortal 0x2 If the plugin supports this feature then the isBehindCaptivePortal property will provide useful results. Otherwise it will always return false .
QNetworkInformation::Feature::TransportMedium 0x4 If the plugin supports this feature then the transportMedium property will provide useful results. Otherwise it will always return TransportMedium::Unknown 。另请参阅 QNetworkInformation::TransportMedium .
QNetworkInformation::Feature::Metered 0x8 If the plugin supports this feature then the isMetered property will provide useful results. Otherwise it will always return false .

The Features type is a typedef for QFlags <Feature>. It stores an OR combination of Feature values.

enum class QNetworkInformation:: Reachability

常量 描述
QNetworkInformation::Reachability::Unknown 0 If this value is returned then we may be connected but the OS has still not confirmed full connectivity, or this feature is not supported.
QNetworkInformation::Reachability::Disconnected 1 Indicates that the system may have no connectivity at all.
QNetworkInformation::Reachability::Local 2 Indicates that the system is connected to a network, but it might only be able to access devices on the local network.
QNetworkInformation::Reachability::Site 3 Indicates that the system is connected to a network, but it might only be able to access devices on the local subnet or an intranet.
QNetworkInformation::Reachability::Online 4 Indicates that the system is connected to a network and able to access the Internet.

另请参阅 QNetworkInformation::reachability .

[since 6.3] enum class QNetworkInformation:: TransportMedium

Lists the currently recognized media with which one can connect to the internet.

常量 描述
QNetworkInformation::TransportMedium::Unknown 0 Returned if either the OS reports no active medium, the active medium is not recognized by Qt, or the TransportMedium feature is not supported.
QNetworkInformation::TransportMedium::Ethernet 1 Indicates that the currently active connection is using ethernet. Note: This value may also be returned when Windows is connected to a Bluetooth personal area network.
QNetworkInformation::TransportMedium::Cellular 2 Indicates that the currently active connection is using a cellular network.
QNetworkInformation::TransportMedium::WiFi 3 Indicates that the currently active connection is using Wi-Fi.
QNetworkInformation::TransportMedium::Bluetooth 4 Indicates that the currently active connection is connected using Bluetooth.

该枚举在 Qt 6.3 引入。

另请参阅 QNetworkInformation::transportMedium .

特性文档编制

[read-only, since 6.2] isBehindCaptivePortal : const bool

Lets you know if the user's device is behind a captive portal.

This property indicates if the user's device is currently known to be behind a captive portal. This functionality relies on the operating system's detection of captive portals and is not supported on systems that don't report this. On systems where this is not supported this will always return false .

该特性在 Qt 6.2 引入。

访问函数:

bool isBehindCaptivePortal () const

通知程序信号:

void isBehindCaptivePortalChanged (bool state )

[read-only, since 6.3] isMetered : const bool

Check if the current connection is metered

This property returns whether the current connection is (known to be) metered or not. You can use this as a guiding factor to decide whether your application should perform certain network requests or uploads. For instance, you may not want to upload logs or diagnostics while this property is true .

This property was introduced in Qt 6.3.

访问函数:

bool isMetered () const

通知程序信号:

void isMeteredChanged (bool isMetered )

[read-only] reachability : const Reachability

This property holds the current state of the system's network connectivity.

Indicates the level of connectivity that can be expected. Do note that this is only based on what the plugin/operating system reports. In certain scenarios this is known to be wrong. For example, on Windows the 'Online' check, by default, is performed by Windows connecting to a Microsoft-owned server. If this server is for any reason blocked then it will assume it does not have Online reachability. Because of this you should not use this as a pre-check before attempting to make a connection.

访问函数:

QNetworkInformation::Reachability reachability () const

通知程序信号:

void reachabilityChanged (QNetworkInformation::Reachability newReachability )

[read-only, since 6.3] transportMedium : const TransportMedium

This property holds the currently active transport medium for the application

This property returns the currently active transport medium for the application, on operating systems where such information is available.

When the current transport medium changes a signal is emitted, this can, for instance, occur when a user leaves the range of a WiFi network, unplugs their ethernet cable or enables Airplane mode.

This property was introduced in Qt 6.3.

访问函数:

QNetworkInformation::TransportMedium transportMedium () const

通知程序信号:

void transportMediumChanged (QNetworkInformation::TransportMedium current )

成员函数文档编制

[static] QStringList QNetworkInformation:: availableBackends ()

Returns a list of the names of all currently available backends.

QString QNetworkInformation:: backendName () const

Returns the name of the currently loaded backend.

[static] QNetworkInformation *QNetworkInformation:: 实例 ()

Returns a pointer to the instance of the QNetworkInformation ,若有的话。

另请参阅 load ().

[static, since 6.4] bool QNetworkInformation:: loadBackendByFeatures ( QNetworkInformation::Features features )

Load a backend which supports features .

返回 true if it managed to load the requested backend or if it was already loaded. Returns false 否则。

该函数在 Qt 6.4 引入。

另请参阅 实例 .

[static, since 6.4] bool QNetworkInformation:: loadBackendByName ( QStringView backend )

Attempts to load a backend whose name matches backend (case insensitively).

返回 true if it managed to load the requested backend or if it was already loaded. Returns false 否则。

该函数在 Qt 6.4 引入。

另请参阅 实例 .

[static, since 6.3] bool QNetworkInformation:: loadDefaultBackend ()

Attempts to load the platform-default backend.

注意: Starting with 6.7 this tries to load any backend that supports Reachability if the platform-default backend is not available or fails to load. If this also fails it will fall back to a backend that only returns the default values for all properties.

This platform-to-plugin mapping is as follows:

平台 Plugin-name
Windows networklistmanager
Apple (macOS/iOS) scnetworkreachability
Android android
Linux networkmanager

This function is provided for convenience where the logic earlier is good enough. If you require a specific plugin then you should call loadBackendByName () 或 loadBackendByFeatures () directly instead.

Determines a suitable backend to load and returns true if this backend is already loaded or on successful loading of it. Returns false if any other backend has already been loaded, or if loading of the selected backend fails.

该函数在 Qt 6.3 引入。

另请参阅 实例 () 和 load ().

[since 6.3] QNetworkInformation::Features QNetworkInformation:: supportedFeatures () const

Returns all the supported features of the current backend.

该函数在 Qt 6.3 引入。

bool QNetworkInformation:: supports ( QNetworkInformation::Features features ) const

返回 true if the currently loaded backend supports features .