In the previous article, it is described that there are 4 commonly used classes related to acquiring native network information, namely: Qhostaddress, Qhostinfo, Qnetworkinterface and Qnetworkaddress Entry. These 4 classes are not described in detail, we mainly use these 4 classes of common methods to obtain information about the network interface.
1#include"mainwindow.h"2 3 #ifdef ir_func_test4#include <QDebug>5#include <QHostInfo>6#include <QHostAddress>7#include <QNetworkInterface>8#include <QNetworkAddressEntry>9 Ten #endif One AMainwindow::mainwindow (Qwidget *parent) - : Qmainwindow (parent) - { the - #ifdef ir_func_test - //Get host name -QString Localhostname =qhostinfo::localhostname (); +Qdebug () <<"Host Name:"<<Localhostname; - + //get a list of IP addresses on this host AQhostinfo Hostinfo =Qhostinfo::fromname (localhostname); atQlist<qhostaddress> addrlist =hostinfo.addresses (); -Qdebug () <<"List of IP addresses on this host, total:"<<Addrlist.count (); - foreach(qhostaddressvar, Addrlist) { -Qdebug () <<var. toString (); - } - in //get a list of network interfaces for this machine -Qlist<qnetworkinterface> ifacelist =qnetworkinterface::allinterfaces (); to for(inti =0; I < Ifacelist.count (); i++){ +Qnetworkinterfacevar=ifacelist.at (i); -Qdebug () << TR ("########## device%1 ############"). Arg (i); theQdebug () << TR ("Interface Name:") <<var. Humanreadablename (); *Qdebug () << TR ("Device Name:") <<var. Name (); $Qdebug () << TR ("Hardware Address:") <<var. hardwareaddress ();Panax Notoginseng - //read a list of associated information for an IP address theQlist<qnetworkaddressentry> entrylist =var. AddressEntries (); + foreach(Qnetworkaddressentry entry, entrylist) { AQdebug () <<"IP:"<<Entry.ip (). toString (); theQdebug () <<"Subnet Mask:"<<Entry.netmask (). toString (); +Qdebug () <<"Broadcast address:"<<entry.broadcast (). toString (); - } $ } $ - #endif - } the -mainwindow::~MainWindow ()Wuyi { the -}
Qt-A comprehensive example of obtaining network interface information