The QNetworkInterface class provides a list of Host IP addresses and network interfaces.
QNetworkInterface indicates a network interface bound to the host when the current program is running. Each network interface may contain 0 or more IP addresses. Each IP address can be selectively associated with a subnet mask and (or) a broadcast address. You can use the QNetworkInterface'sAddressEntries ()Method. As an optional solution, when the subnet mask or broadcast address is not required, you can useAllAddresses ()A convenient function is used to obtain only the IP address.
Note: The QNetworkAddressEntry class stores an IP address supported by the network interface, along with the associated subnet mask and broadcast address.
QNetworkInterface is also usedHardwareAddress ()The hardware address of the method report interface.
Not all operating systems support reporting all of these features. Only the IPv4 address can be listed on all platforms. In particular, the list of IPv6 addresses currently only supports Windows XP and related versions, Linux, MacOS, and BSDs.
References:QNetworkAddressEntry.
Member function: allAddresses () [static]
This convenience function returns all IP addresses found on the host. It is equivalentAllInterfaces ()Call all returned objectsAddressEntries ()To obtain the QHostAddress Object List, and then callQHostAddress: ip ()Method.
1 #include "mainwindow.h" 2 #include <QHostAddress> 3 #include <QNetworkInterface> 4 #include <QNetworkAddressEntry> 5 #include <QDebug> 6 7 MainWindow::MainWindow(QWidget *parent) 8 : QMainWindow(parent) 9 {10 QList<QHostAddress> list = QNetworkInterface::allAddresses();11 12 for (int i = 0; i < list.size(); i++)13 {14 qDebug() << list.at(i);15 }16 }
Member functions: allInterfaces () [static]
Returns a list of all network interfaces found on the host. If the search 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 7 MainWindow::MainWindow(QWidget *parent) 8 : QMainWindow(parent) 9 {10 QList<QNetworkInterface> interface = QNetworkInterface::allInterfaces();11 12 for (int i = 0; i < interface.size(); i++)13 {14 qDebug() << interface.at(i);15 QNetworkInterface item = interface.at(i);16 QList<QNetworkAddressEntry> entryList = item.addressEntries();17 for (int j = 0; j < entryList.size(); j++)18 {19 qDebug() << "Item #" << j << entryList.at(j).ip();20 }21 }22 }
Member function: hardwareAddress () const
Returns the underlying hardware address of this interface. For Ethernet interfaces, This is a MAC address represented by a string, which is separated by a colon. Other interface types may use other types of hardware addresses. 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 7 MainWindow::MainWindow(QWidget *parent) 8 : QMainWindow(parent) 9 {10 QList<QNetworkInterface> interface = QNetworkInterface::allInterfaces();11 12 for (int i = 0; i < interface.size(); i++)13 {14 QNetworkInterface item = interface.at(i);15 qDebug() << item.hardwareAddress();16 }17 }
Reference: Obtain Network Interface Information-MyNote