Qt开发——获取本机网络信息软件

目录

效果图

思路:QHostInfo+QNewworkInterface

注意:在.pro中添加QT+=network并保存!

network.cpp

network.h


效果图

思路:QHostInfo+QNewworkInterface

注意:在.pro中添加QT+=network并保存!

network.cpp

#include "networkinformation.h"
#include <QHostInfo>
#include <QNetworkInterface>


NetworkInformation::NetworkInformation(QWidget *parent)
    : QWidget(parent)
{
    //布局设计
    setWindowTitle(QStringLiteral("本机网络信息"));
    setWindowIcon(QIcon("icon.png"));
    resize(400,100);
    hostLabel = new QLabel(QStringLiteral("主机名:"));
    LineEditLocalHostName = new QLineEdit;
    ipLabel = new QLabel(QStringLiteral("IP 地址:"));
    LineEditAddress = new QLineEdit;
    detailBtn = new QPushButton(QStringLiteral("详细"));
    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,2,2);
    //获取本机网络信息
    getHostInformation();
    connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotDetail()));

}

NetworkInformation::~NetworkInformation()
{

}

//获取本机网络信息
void NetworkInformation::getHostInformation(){
    //获取本机主机名
    QString localHostName = QHostInfo::localHostName();
    LineEditLocalHostName->setText(localHostName);
//    LineEditLocalHostName->setText(QStringLiteral("1123456"));
    //获取IP地址
    QHostInfo hostInfo = QHostInfo::fromName(localHostName);
    //获取主机IP地址列表
    QList<QHostAddress> listAddress = hostInfo.addresses();
//    LineEditAddress->setText(listAddress.at(1).toString());
    if(!listAddress.isEmpty()){
        LineEditAddress->setText(listAddress.at(1).toString());
    }
}

//详细按键获取网络接口相关信息
void NetworkInformation::slotDetail(){
    QString detail="";
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();//获取IP地址和网络地址列表

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

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

    QMessageBox::information(this,QStringLiteral("详细信息"),detail);
}

network.h

#ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QGridLayout>
#include <QPushButton>
#include <QtNetwork/QHostInfo>
#include <QtNetwork/QNetworkInterface>

class NetworkInformation : public QWidget
{
    Q_OBJECT

public:
    NetworkInformation(QWidget *parent = nullptr);
    ~NetworkInformation();
    void getHostInformation();//获取本机网络信息

public slots:
    void slotDetail();

private:
    QLabel *hostLabel;
    QLineEdit *LineEditLocalHostName;
    QLabel *ipLabel;
    QLineEdit *LineEditAddress;
    QPushButton *detailBtn;
    QGridLayout *mainLayout;
    
};

#endif // NETWORKINFORMATION_H
发布了242 篇原创文章 · 获赞 237 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_41895747/article/details/104078172
今日推荐