QNetworkInterface 类

QNetworkInterface 类提供主机 IP 地址和网络接口的列表。 更多...

头: #include <QNetworkInterface>
qmake: QT += network
Since: Qt 4.2

注意: 此类的所有函数 可重入 .

公共类型

enum InterfaceFlag { IsUp, IsRunning, CanBroadcast, IsLoopBack, IsPointToPoint, CanMulticast }
flags InterfaceFlags

公共函数

QNetworkInterface ()
QNetworkInterface (const QNetworkInterface & other )
~QNetworkInterface ()
QList<QNetworkAddressEntry> addressEntries () const
InterfaceFlags flags () const
QString hardwareAddress () const
QString humanReadableName () const
int index () const
bool isValid () const
QString name () const
void swap (QNetworkInterface & other )
QNetworkInterface & operator= (QNetworkInterface && other )
QNetworkInterface & operator= (const QNetworkInterface & other )

静态公共成员

QList<QHostAddress> allAddresses ()
QList<QNetworkInterface> allInterfaces ()
QNetworkInterface interfaceFromIndex (int index )
QNetworkInterface interfaceFromName (const QString & name )
int interfaceIndexFromName (const QString & name )
QString interfaceNameFromIndex (int index )

详细描述

QNetworkInterface 类提供主机 IP 地址和网络接口的列表。

QNetworkInterface represents one network interface attached to the host where the program is being run. Each network interface may contain zero or more IP addresses, each of which is optionally associated with a netmask and/or a broadcast address. The list of such trios can be obtained with addressEntries (). Alternatively, when the netmask or the broadcast addresses aren't necessary, use the allAddresses () convenience function to obtain just the IP addresses.

QNetworkInterface also reports the interface's hardware address with hardwareAddress ().

Not all operating systems support reporting all features. Only the IPv4 addresses are guaranteed to be listed by this class in all platforms. In particular, IPv6 address listing is only supported on Windows, Linux, macOS and the BSDs.

另请参阅 QNetworkAddressEntry .

成员类型文档编制

enum QNetworkInterface:: InterfaceFlag
flags QNetworkInterface:: InterfaceFlags

指定关联此网络接口的标志。可能的值:

常量 描述
QNetworkInterface::IsUp 0x1 网络接口是活动的
QNetworkInterface::IsRunning 0x2 the network interface has resources allocated
QNetworkInterface::CanBroadcast 0x4 the network interface works in broadcast mode
QNetworkInterface::IsLoopBack 0x8 the network interface is a loopback interface: that is, it's a virtual interface whose destination is the host computer itself
QNetworkInterface::IsPointToPoint 0x10 the network interface is a point-to-point interface: that is, there is one, single other address that can be directly reached by it.
QNetworkInterface::CanMulticast 0x20 the network interface supports multicasting

Note that one network interface cannot be both broadcast-based and point-to-point.

InterfaceFlags 类型是 typedef 对于 QFlags <InterfaceFlag>. It stores an OR combination of InterfaceFlag values.

成员函数文档编制

QNetworkInterface:: QNetworkInterface ()

构造空的网络接口对象。

QNetworkInterface:: QNetworkInterface (const QNetworkInterface & other )

创建副本为 QNetworkInterface 对象包含在 other .

QNetworkInterface:: ~QNetworkInterface ()

释放资源关联 QNetworkInterface 对象。

QList < QNetworkAddressEntry > QNetworkInterface:: addressEntries () const

Returns the list of IP addresses that this interface possesses along with their associated netmasks and broadcast addresses.

If the netmask or broadcast address information is not necessary, you can call the allAddresses () function to obtain just the IP addresses.

[static] QList < QHostAddress > QNetworkInterface:: allAddresses ()

此方便函数返回在主机中找到的所有 IP 地址。相当于调用 addressEntries () on all the objects returned by allInterfaces () to obtain lists of QHostAddress objects then calling QHostAddress::ip() on each of these.

[static] QList < QNetworkInterface > QNetworkInterface:: allInterfaces ()

Returns a listing of all the network interfaces found on the host machine. In case of failure it returns a list with zero elements.

InterfaceFlags QNetworkInterface:: flags () const

返回此网络接口关联的标志。

QString QNetworkInterface:: hardwareAddress () const

Returns the low-level hardware address for this interface. On Ethernet interfaces, this will be a MAC address in string representation, separated by colons.

Other interface types may have other types of hardware addresses. Implementations should not depend on this function returning a valid MAC address.

QString QNetworkInterface:: humanReadableName () const

Returns the human-readable name of this network interface on Windows, such as "Local Area Connection", if the name could be determined. If it couldn't, this function returns the same as name (). The human-readable name is a name that the user can modify in the Windows Control Panel, so it may change during the execution of the program.

On Unix, this function currently always returns the same as name (), since Unix systems don't store a configuration for human-readable names.

该函数在 Qt 4.5 引入。

int QNetworkInterface:: index () const

Returns the interface system index, if known. This is an integer assigned by the operating system to identify this interface and it generally doesn't change. It matches the scope ID field in IPv6 addresses.

此函数返回 0,若索引未知。

该函数在 Qt 4.5 引入。

[static] QNetworkInterface QNetworkInterface:: interfaceFromIndex ( int index )

返回 QNetworkInterface object for the interface whose internal ID is index . Network interfaces have a unique identifier called the "interface index" to distinguish it from other interfaces on the system. Often, this value is assigned progressively and interfaces being removed and then added again get a different value every time.

This index is also found in the IPv6 address' scope ID field.

[static] QNetworkInterface QNetworkInterface:: interfaceFromName (const QString & name )

返回 QNetworkInterface object for the interface named name . If no such interface exists, this function returns an invalid QNetworkInterface 对象。

字符串 name may be either an actual interface name (such as "eth0" or "en1") or an interface index in string form ("1", "2", etc.).

另请参阅 name () 和 isValid ().

[static] int QNetworkInterface:: interfaceIndexFromName (const QString & name )

Returns the index of the interface whose name is name or 0 if there is no interface with that name. This function should produce the same result as the following code, but will probably execute faster.

QNetworkInterface::interfaceFromName(name).index()
					

该函数在 Qt 5.7 引入。

另请参阅 interfaceFromName (), interfaceNameFromIndex (),和 QNetworkDatagram::interfaceIndex ().

[static] QString QNetworkInterface:: interfaceNameFromIndex ( int index )

Returns the name of the interface whose index is index or an empty string if there is no interface with that index. This function should produce the same result as the following code, but will probably execute faster.

QNetworkInterface::interfaceFromIndex(index).name()
					

该函数在 Qt 5.7 引入。

另请参阅 interfaceFromIndex (), interfaceIndexFromName (),和 QNetworkDatagram::interfaceIndex ().

bool QNetworkInterface:: isValid () const

返回 true 若此 QNetworkInterface object contains valid information about a network interface.

QString QNetworkInterface:: name () const

Returns the name of this network interface. On Unix systems, this is a string containing the type of the interface and optionally a sequence number, such as "eth0", "lo" or "pcn0". On Windows, it's an internal ID that cannot be changed by the user.

void QNetworkInterface:: swap ( QNetworkInterface & other )

Swaps this network interface instance with other 。此函数非常快,且从不失败。

该函数在 Qt 5.0 引入。

QNetworkInterface &QNetworkInterface:: operator= ( QNetworkInterface && other )

移动赋值运算符。

QNetworkInterface &QNetworkInterface:: operator= (const QNetworkInterface & other )

拷贝内容从 QNetworkInterface 对象包含在 other 到此。