使用qt的hostInfo类,查看本机的IP和设备

创建NetWorkInformation类,main.cpp直接生成。

 1 #include "networkinformation.h"
 2 #include <QApplication>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication a(argc, argv);
 7     NetworkInformation w;
 8     w.show();
 9 
10     return a.exec();
11 }

头文件声明要写的槽函数,还有布局控件。

 1 #ifndef NETWORKINFORMATION_H
 2 #define NETWORKINFORMATION_H
 3 
 4 #include <QWidget>
 5 #include <QHostInfo>
 6 #include <QNetworkInterface>
 7 #include <QLabel>
 8 #include <QLineEdit>
 9 #include <QPushButton>
10 #include <QGridLayout>
11 class NetworkInformation : public QWidget
12 {
13     Q_OBJECT
14 
15 public:
16     NetworkInformation(QWidget *parent = 0);
17     ~NetworkInformation();
18 public:
19     void getHostInformation();
20 public slots:
21     void slotDetail();
22 private:
23     QLabel *hostLabel;
24     QLineEdit *LineEditLocalHostname;
25     QLabel * ipLabel;
26     QLineEdit * LineEditAddress;
27     QPushButton* detailBtn;
28     QGridLayout * mainLayout;
29 };
30 
31 #endif // NETWORKINFORMATION_H

最后是.cpp的文件。

在构造函数里写控件的布局,然后实现槽函数。

 1 #include "networkinformation.h"
 2 #include <QMessageBox>
 3 NetworkInformation::NetworkInformation(QWidget *parent)
 4     : QWidget(parent)
 5 {
 6    hostLabel = new QLabel("name:");
 7    LineEditLocalHostname = new QLineEdit;
 8    ipLabel = new QLabel(tr("ip:"));
 9    LineEditAddress = new QLineEdit;
10    detailBtn = new QPushButton(tr("detail"));
11    mainLayout =new QGridLayout(this);
12 
13    mainLayout->addWidget(hostLabel,0,0);
14    mainLayout->addWidget(LineEditLocalHostname,0,1);
15    mainLayout->addWidget(ipLabel,1,0);
16    mainLayout->addWidget(LineEditAddress,1,1);
17    mainLayout->addWidget(detailBtn,2,0,1,2);
18    getHostInformation();
19    connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotDetail()));
20 }
21 
22 NetworkInformation::~NetworkInformation()
23 {
24 
25 }
26 void NetworkInformation::getHostInformation()
27 {
28    QString localHostName = QHostInfo::localHostName();
29    LineEditLocalHostname->setText(localHostName);
30 
31    QHostInfo hostinfo = QHostInfo::fromName(localHostName);
32 
33    foreach (const QHostAddress &address, hostinfo.addresses())
34     {
35 
36         if (address.protocol() == QAbstractSocket::IPv4Protocol)
37        {
38         LineEditAddress->setText((address.toString()));
39        }
40     }
41 }
42 void NetworkInformation::slotDetail()
43 {
44     QString detail="123";
45    QList<QNetworkInterface>list=QNetworkInterface::allInterfaces();
46     for(int i=0;i<list.count();i++)
47    {
48       QNetworkInterface interface = list.at(i);
49       detail= detail+tr("设备:")+interface.name()+"
";
50        detail= detail+tr("硬件地址:")+interface.hardwareAddress()+"
";
51        QList<QNetworkAddressEntry>entryList = interface.addressEntries();
52       for(int j =0;j<entryList.count();j++)
53        {
54          /QNetworkAddressEntry entry = entryList.at(i);
55           detail = detail + "	" +tr("IP 地址:")+entry.ip().toString()+"
";
56           detail = detail + "	" + tr("子网掩码:")+entry.netmask().toString()+"
";
57           detail = detail + "	" +tr("广播地址:") +entry.broadcast().toString()+"
";
58        }
59     }
60     QMessageBox::information(this,tr("Detail"),detail);
61 }
原文地址:https://www.cnblogs.com/132818Creator/p/7227514.html