The Qnetworkinterface class provides a list of host IP addresses and network interfaces.
Qnetworkinterface represents a network interface that is bound to the host when the current program is running . Each network interface may contain 0 or more IP addresses, and each IP address can optionally be associated with a subnet mask and/or a broadcast address. Such a list can be obtained by Qnetworkinterface's addressentries () method. As an alternative, when the subnet mask or broadcast address is not required, you can use the alladdresses () convenience function to obtain only the IP address.
Note: The Qnetworkaddressentry class stores an IP address that is supported by the network interface, along with its associated subnet mask and broadcast address.
Qnetworkinterface also uses the hardwareaddress () method to report the hardware address of the interface.
Not all operating systems support reporting all of these features. Only IPV4 addresses are guaranteed to be listed on all platforms, especially IPV6 addresses are currently supported only for Windows XP and related editions, Linux, MacOS, and BSDs.
Related references: Qnetworkaddressentry.
member function:alladdresses () [Static]
This convenience function returns all the IP addresses found on the host. It is equivalent to calling AddressEntries () for all objects returned by allinterfaces () to get a list of Qhostaddress objects and then calling Qhostaddress on these objects : IP () method.
1#include"mainwindow.h"2#include <QHostAddress>3#include <QNetworkInterface>4#include <QNetworkAddressEntry>5#include <QDebug>6 7Mainwindow::mainwindow (Qwidget *parent)8 : Qmainwindow (parent)9 {Tenqlist<qhostaddress> list =qnetworkinterface::alladdresses (); One A for(inti =0; I < list.size (); i++) - { -Qdebug () <<list.at (i); the } -}
member function:allinterfaces () [Static]
Returns a list of all network interfaces found on the host. If the lookup fails, a list of 0 elements is returned.
1#include"mainwindow.h"2#include <QHostAddress>3#include <QNetworkInterface>4#include <QNetworkAddressEntry>5#include <QDebug>6 7Mainwindow::mainwindow (Qwidget *parent)8 : Qmainwindow (parent)9 {TenQlist<qnetworkinterface>Interface=qnetworkinterface::allinterfaces (); One A for(inti =0; I <Interface. Size (); i++) - { -Qdebug () <<Interface. at (i); theQnetworkinterface item =Interface. at (i); -Qlist<qnetworkaddressentry> entrylist =item.addressentries (); - for(intj =0; J < Entrylist.size (); J + +) - { +Qdebug () <<"Item #"<< J <<entrylist.at (j). IP (); - } + } A}
member function:hardwareaddress () const
Returns the underlying hardware address for this interface. For an Ethernet interface, this will be a string representation of the MAC address, separated by a colon. Other types of hardware addresses may be used by other interface types. Implementation do not rely on this function to return a valid MAC address.
1#include"mainwindow.h"2#include <QHostAddress>3#include <QNetworkInterface>4#include <QNetworkAddressEntry>5#include <QDebug>6 7Mainwindow::mainwindow (Qwidget *parent)8 : Qmainwindow (parent)9 {TenQlist<qnetworkinterface>Interface=qnetworkinterface::allinterfaces (); One A for(inti =0; I <Interface. Size (); i++) - { -Qnetworkinterface item =Interface. at (i); theQdebug () <<item.hardwareaddress (); - } -}
Reference: "Acquiring network interface information"-mynote