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

效果展现

在这里插入图片描述
在这里插入图片描述

代码实现

networkinformation.h

#ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H

#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>


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 <QGridLayout>
#include <QHostInfo>
#include <QNetworkInterface>
#include <QMessageBox>
#include <QDebug>

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 <QApplication>

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值