Qt——实现一个获取本机网络信息的界面

效果展现

Qt——实现一个获取本机网络信息的界面_第1张图片
Qt——实现一个获取本机网络信息的界面_第2张图片

代码实现

networkinformation.h

#ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H

#include 
#include 
#include 
#include 


class NetworkInformation : public QMainWindow
{
    Q_OBJECT

public:
    NetworkInformation(QWidget *parent = nullptr);
    ~NetworkInformation();

    void getHostInfo();

public slots:
    void slotDetail();


private:
   QLabel * hostNameLabel;
   QLineEdit * hostNameLineEdit;

   QLabel * ipLabel;
   QLineEdit * ipLineEdit;

   QPushButton * detailBtn;

};
#endif // NETWORKINFORMATION_H


networkinformation.cpp

#include "networkinformation.h"
#include 
#include 
#include 
#include 
#include 

NetworkInformation::NetworkInformation(QWidget *parent)
    : QMainWindow(parent)

{
    QWidget * widget = new QWidget();
    this->setCentralWidget(widget);


    hostNameLabel = new QLabel(tr("主机名:"));
    hostNameLineEdit = new QLineEdit;

    ipLabel = new QLabel(tr("IP地址:"));
    ipLineEdit = new QLineEdit;

    detailBtn = new QPushButton(tr("详细"));

    QGridLayout * mainLayout = new QGridLayout;
    mainLayout->addWidget(hostNameLabel,0,0);
    mainLayout->addWidget(hostNameLineEdit,0,1);
    mainLayout->addWidget(ipLabel,1,0);
    mainLayout->addWidget(ipLineEdit,1,1);
    mainLayout->addWidget(detailBtn,2,0,1,2);

    widget->setLayout(mainLayout);

    //获取主机信息
    getHostInfo();

    //点击详细按钮 显示详细的 网络信息
    connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotDetail()));

}

void NetworkInformation::getHostInfo(){
    //主机名
    QString hostName = QHostInfo::localHostName();
    hostNameLineEdit->setText(hostName);

    //ip地址
    QHostInfo hostInfo = QHostInfo::fromName(hostName);
    QList<QHostAddress> list = hostInfo.addresses();

    qDebug()<<"size = "<<list.count()<<endl;
    for(int i = 0;i < list.count();i++){
        qDebug()<<list.at(i).toString()<<endl;
    }

    if(list.count() >= 3){
        ipLineEdit->setText(list.at(3).toString());
    }

}

void NetworkInformation::slotDetail(){
    QString detail = "";
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();

    for(int i = 0;i < list.count();i++){
        QNetworkInterface interface = list.at(i);
        detail += tr("设备:") + interface.name() + "\n";
        detail += tr("硬件地址:") + interface.hardwareAddress() + "\n";

        QList<QNetworkAddressEntry> entryList = interface.addressEntries();

        for(int j = 0;j < entryList.count();j++){
            QNetworkAddressEntry entry = entryList.at(j);
            detail += "\t" + tr("IP地址:") + entry.ip().toString() + "\n";
            detail += "\t" + tr("子网掩码:") + entry.netmask().toString() + "\n";
            detail += "\t" + tr("广播地址:") + entry.broadcast().toString() + "\n";
        }
    }

   qDebug()<<detail<<endl;
   QMessageBox::information(this,tr("Detail"),detail);

}


NetworkInformation::~NetworkInformation()
{
}



main.cpp

#include "networkinformation.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    NetworkInformation w;
    w.show();
    return a.exec();
}


你可能感兴趣的:(QT,qt,开发语言)