Roson的Qt之旅 #123 QNetworkConfigurationManager网络配置管理
CSDN话题挑战赛第2期
参赛话题:Qt应用程序开发
1.详细描述
QNetworkConfigurationManager类管理着系统所提供的网络配置。
QNetworkConfigurationManager提供了对系统已知的网络配置的访问,使应用程序能够在运行时检测系统的能力(关于网络会话)。
一个QNetworkConfiguration抽象了一组配置选项,描述了如何配置网络接口以连接到一个特定的目标网络。QNetworkConfigurationManager维护和更新QNetworkConfigurations的全球列表。应用程序可以通过 allConfigurations() 访问和过滤这个列表。如果一个新的配置被添加,或者一个现有的配置被删除或改变,配置添加()、配置删除()和配置改变()信号将被分别发出。
defaultConfiguration()可以在打算立即创建一个新的网络会话而不关心特定的配置时使用。它返回一个QNetworkConfiguration::Discovered配置。如果没有任何发现的配置,则返回无效的配置。
一些配置的更新可能需要一些时间来执行更新。WLAN扫描就是这样一个例子。除非平台执行内部更新,否则可能需要通过QNetworkConfigurationManager::updateConfigurations()手动触发配置更新。更新过程的完成是通过发出 updateCompleted() 信号来表示的。更新过程确保每个现有的QNetworkConfiguration实例都被更新。没有必要通过 allConfigurations() 要求更新配置列表。
请参阅 QNetworkConfiguration。
2.成员类型说明
enum QNetworkConfigurationManager::Capability flags QNetworkConfigurationManager::Capabilities
指定承载API的系统能力。可能的值是:
Constant | Value | Description |
---|---|---|
QNetworkConfigurationManager::CanStartAndStopInterfaces | 0x00000001 | Network sessions and their underlying access points can be started and stopped. If this flag is not set QNetworkSession can only monitor but not influence the state of access points. On some platforms this feature may require elevated user permissions. This option is platform specific and may not always be available. |
QNetworkConfigurationManager::DirectConnectionRouting | 0x00000002 | Network sessions and their sockets can be bound to a particular network interface. Any packet that passes through the socket goes to the specified network interface and thus disregards standard routing table entries. This may be useful when two interfaces can reach overlapping IP ranges or an application has specific needs in regards to target networks. This option is platform specific and may not always be available. |
QNetworkConfigurationManager::SystemSessionSupport | 0x00000004 | If this flag is set the underlying platform ensures that a network interface is not shut down until the last network session has been closed(). This works across multiple processes. If the platform session support is missing this API can only ensure the above behavior for network sessions within the same process. In general mobile platforms have such support whereas most desktop platform lack this capability. |
QNetworkConfigurationManager::ApplicationLevelRoaming | 0x00000008 | The system gives applications control over the systems roaming behavior. Applications can initiate roaming (in case the current link is not suitable) and are consulted if the system has identified a more suitable access point. |
QNetworkConfigurationManager::ForcedRoaming | 0x00000010 | The system disconnects an existing access point and reconnects via a more suitable one. The application does not have any control over this process and has to reconnect its active sockets. |
QNetworkConfigurationManager::DataStatistics | 0x00000020 | If this flag is set QNetworkSession can provide statistics about transmitted and received data. |
QNetworkConfigurationManager::NetworkSessionRequired | 0x00000040 | If this flag is set the platform requires that a network session is created before network operations can be performed. |
Capabilities类型是QFlags<Capability>的一个类型定义。它存储了Capability值的OR组合。
3.成员函数
QNetworkConfigurationManager::QNetworkConfigurationManager(QObject parent* = Q_NULLPTR)**
构建一个具有给定父类的QNetworkConfigurationManager。 请注意,为了确保当前配置的有效列表立即可用,更新是在构建过程中进行的,这会导致一些延迟。
[virtual] QNetworkConfigurationManager::~QNetworkConfigurationManager()
释放与QNetworkConfigurationManager对象相关的资源。
QList<QNetworkConfiguration> QNetworkConfigurationManager::allConfigurations(QNetworkConfiguration::StateFlags filter = QNetworkConfiguration::StateFlags()) const
返回符合给定过滤器的配置的列表。 默认情况下,该函数返回所有(定义和未定义)的配置。 一个具有特定SSID的无线网络可能只在某个区域可以访问,尽管系统对它有一个有效的配置。因此,过滤标志可以用来限制列表中的配置,使其只限于已发现和可能已连接的配置。
如果过滤器被设置为零,该函数将返回所有可能的配置。
请注意,该函数返回所有配置的状态,因为它们在调用该函数时是已知的。例如,如果定义了一个WLAN类型的配置,系统可能必须执行一个WLAN扫描,以确定它是否真的可用。为了获得最准确的状态,应该使用 updateConfigurations() 来更新每个配置的状态。请注意,这种更新可能需要一些时间。它的完成是由updateCompleted()发出的信号。在没有配置更新的情况下,该函数返回调用时的最佳估计值。因此,如果对WLAN配置感兴趣,建议在QNetworkConfigurationManager实例化后调用updateConfigurations()一次(WLAN扫描在构造函数中执行太耗时)。在这之后,随着系统报告任何变化,数据会自动保持最新状态。
QNetworkConfigurationManager::Capabilities QNetworkConfigurationManager::capabilities() const
返回当前平台所支持的能力。
[signal] void QNetworkConfigurationManager::configurationAdded(const QNetworkConfiguration &config)
每当一个新的网络配置被添加到系统中时,这个信号就会被发射出来。新的配置是由config指定的。
[signal] void QNetworkConfigurationManager::configurationChanged(const QNetworkConfiguration &config)
当config的状态发生变化时,这个信号就会被发射出来。
QNetworkConfiguration QNetworkConfigurationManager::configurationFromIdentifier(const QString &identifier) const
返回标识符的QNetworkConfiguration;否则返回一个无效的QNetworkConfiguration。 也请看QNetworkConfiguration::identifier()。
[signal] void QNetworkConfigurationManager::configurationRemoved(const QNetworkConfiguration &config)
当一个配置即将被从系统中移除时,这个信号就会被发出。由config指定的被移除的配置是无效的,但保留了名称和标识符。
QNetworkConfiguration QNetworkConfigurationManager::defaultConfiguration() const
返回要使用的默认配置。这个函数总是返回一个已发现的配置;否则就是无效的配置。 在某些情况下,可能需要调用updateConfigurations()并等待updateCompleted()信号后再调用此函数。 也请看 allConfigurations()。
bool QNetworkConfigurationManager::isOnline() const
如果系统被认为是通过一个活动的网络接口与另一个设备连接,则返回真;否则返回假。 这相当于下面的代码片断:
QNetworkConfigurationManager mgr; QList<QNetworkConfiguration> activeConfigs = mgr.allConfigurations(QNetworkConfiguration::Active); if (activeConfigs.count() > 0) Q_ASSERT(mgr.isOnline()); else Q_ASSERT(!mgr.isOnline());
参见 onlineStateChanged().
[signal] void QNetworkConfigurationManager::onlineStateChanged(bool isOnline)
当设备从在线模式变为离线模式或反之亦然时,这个信号就会发出。 isOnline表示设备的新状态。 只要allConfigurations(QNetworkConfiguration::Active)返回一个至少有一个条目的列表,该状态就被认为是在线的。
[signal] void QNetworkConfigurationManager::updateCompleted()
当配置更新完成时,这个信号被发射出来。这样的更新可以通过 updateConfigurations() 启动。
[slot] void QNetworkConfigurationManager::updateConfigurations()
启动对所有配置的更新。这可用于启动WLAN扫描或其他耗时的更新,这可能需要获得配置的正确状态。 这个调用是异步的。在此更新完成后,会发出 updateCompleted() 信号。如果发现新的配置或旧的配置被删除或更改,更新过程可能会触发一个或多个配置添加()、配置删除()和配置更改()信号的发射。 如果配置状态因该更新而改变,所有现有的QNetworkConfiguration实例都会自动更新。 请参阅 allConfigurations()。