"QT5 Development and examples" 28, access to local network information

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.