Networkinformation.h
/*** Book: "QT5 Development and examples" * Function: Get native Network information * file: networkinformation.h* time: February 5, 2015 14:51:33* Author: cutter_point*/#ifndef Networkinformation_h#define networkinformation_h#include <QWidget> #include <QLabel> #include < qlineedit> #include <QPushButton> #include <QGridLayout> #include <QHostInfo> #include < qnetworkinterface> #include <qmessagebox>class networkinformation:public qwidget{ Q_OBJECTpublic: networkinformation (Qwidget *parent = 0); ~networkinformation (); void Gethostinformation (); Get host Information public slots: void Slotdetail (); Show Details Private: //Interface layout Qlabel *hostlabel; Qlineedit *lineeditlocalhostname; Qlabel *iplabel; Qlineedit *lineeditaddress; Qpushbutton *detailbtn; Qgridlayout *mainlayout;}; #endif//Networkinformation_h
Networkinformation.cpp
/*** Book: "QT5 Development and examples" * Function: Get native Network information * file: networkinformation.cpp* time: February 5, 2015 14:51:33* Author: cutter_point*/#include " Networkinformation.h "Networkinformation::networkinformation (Qwidget *parent): Qwidget (parent) {Hostlabel = new Qlabel (TR ("host name:")); Lineeditlocalhostname = new Qlineedit; Iplabel = new Qlabel (tr ("IP Address:")); lineeditaddress = new Qlineedit; DETAILBTN = new Qpushbutton (tr ("verbose")); Mainlayout = new Qgridlayout (this); Mainlayout->addwidget (hostlabel, 0, 0); Mainlayout->addwidget (lineeditlocalhostname, 0, 1); Mainlayout->addwidget (Iplabel, 1, 0); Mainlayout->addwidget (lineeditaddress, 1, 1); Mainlayout->addwidget (DETAILBTN, 2, 0, 1, 2); Gethostinformation (); Connect (detailbtn, SIGNAL (clicked ()), this, SLOT (Slotdetail ()));} void Slotdetail (); Show details void Networkinformation::slotdetail () {QString detail = ""; qlist<qnetworkinterface> list = Qnetworkinterface::allinterfaces (); Get all interfaces for (int i = 0; i < List.count (); ++i) {QNetWorkinterface interface = list.at (i); Get the interface of I//detail = detail + TR ("Device:") + interface.name () + "\ n"; DETAIL=DETAIL+TR ("Device:") +interface.name () + "\ n"; The name of the network interface DETAIL=DETAIL+TR ("Hardware address:") +interface.hardwareaddress () + "\ n"; The hardware address of the network interface qlist<qnetworkaddressentry> entrylist = Interface.addressentries (); for (int j = 0; J < Entrylist.count (); ++j) {Qnetworkaddressentry entry = entrylist.at (j); Detail = detail + "\ T" + TR ("IP address:") + Entry.ip (). toString () + "\ n"; Detail = detail + "\ T" + TR ("Subnet mask:") + Entry.netmask (). toString () + "\ n"; Detail = detail + "\ T" + TR ("Broadcast address:") + Entry.broadcast (). toString () + "\ n"; } qmessagebox::information (This, tr ("Detail"), Detail); }}//void gethostinformation (); Get host information void Networkinformation::gethostinformation () {QString localhostname = Qhostinfo::localhostname (); Get host name Lineeditlocalhostname->settext (Localhostname); Qhostinfo Hostinfo = QhostinfO::fromname (Localhostname); qlist<qhostaddress> listaddress = hostinfo.addresses (); Host all Addresses if (!listaddress.isempty ()) {//listaddress.at (2)//Lineeditaddress->settext (Listaddress.first (). ToString ()); Take the first//Lineeditaddress->settext (Static_cast<qstring> (Listaddress.length ())); Take the first Lineeditaddress->settext (listaddress.at (3). ToString ()); }}networkinformation::~networkinformation () {}
Results show
"QT5 Development and examples" 28, access to local network information