一.概述

查询一个主机的MAC地址或者IP地址是网络应用中常用到的功能,Qt提供了QHostInfo和QNetworkInterface 类可以用于此类信息的查询

1.QHostInfo 类(显示和查找本地的信息)

2.QNetworkInterface 类(获得应用程序上所在主机的所有网络接口,包括子网掩码和广播地址)

 推荐一个不错的人工智能学习网站,通俗易懂,内容全面,作为入门科普和学习提升都不错,分享一下给大家:https://www.captainbed.cn/ai

二.代码实现

1.dialog.h

#ifndef DIALOG_H

#define DIALOG_H

#include    <QDialog>

#include    <QHostInfo>

namespace Ui {

class Dialog;

}

class Dialog : public QDialog

{

    Q_OBJECT

public:

    explicit Dialog(QWidget *parent = 0);

    ~Dialog();

private slots:

    void lookedUpHostInfo(const QHostInfo &host);

    void on_btnGetHostInfo_clicked();

    void on_btnDetail_clicked();

    void on_btnLookup_clicked();

    void on_btnALLInterface_clicked();

    void on_btnClear_clicked();

private:

    Ui::Dialog *ui;

    QString  protocolName(QAbstractSocket::NetworkLayerProtocol protocol);

};

#endif // DIALOG_H

2.dialog.cpp

#include "dialog.h"

#include "ui_dialog.h"

#include    <QHostInfo>

#include    <QNetworkInterface>

Dialog::Dialog(QWidget *parent) :

    QDialog(parent),

    ui(new Ui::Dialog)

{

    ui->setupUi(this);

}

Dialog::~Dialog()

{

    delete ui;

}

void Dialog::lookedUpHostInfo(const QHostInfo &host)

{//查找主机信息的槽函数

    QList<QHostAddress> addList=host.addresses();//

    if (!addList.isEmpty())

    for (int i=0;i<addList.count();i++)

    {

        QHostAddress aHost=addList.at(i);

        bool show=ui->chkOnlyIPv4->isChecked();//只显示IPv4

        if (show)

            show=QAbstractSocket::IPv4Protocol==aHost.protocol();

        else

            show=true;

        if (show) {

        ui->plainTextEdit->appendPlainText("协 议:"+protocolName(aHost.protocol()));

        ui->plainTextEdit->appendPlainText(aHost.toString());

        }

    }

}

void Dialog::on_btnGetHostInfo_clicked()

{//QHostInfo获取主机信息

    QString hostName=QHostInfo::localHostName();//本地主机名

    ui->plainTextEdit->appendPlainText("本机主机名:"+hostName+"\n");

    QHostInfo   hostInfo=QHostInfo::fromName(hostName); //本机IP地址

    QList<QHostAddress> addList=hostInfo.addresses();//IP地址列表

    if (!addList.isEmpty())

    for (int i=0;i<addList.count();i++)

    {

        QHostAddress aHost=addList.at(i); //每一项是一个QHostAddress

        bool show=ui->chkOnlyIPv4->isChecked();//只显示IPv4

        if (show)

            show=(QAbstractSocket::IPv4Protocol==aHost.protocol()); //协议类型,

        else

            show=true;

        if (show) {

        ui->plainTextEdit->appendPlainText("协 议:"+protocolName(aHost.protocol()));//协议类型

        ui->plainTextEdit->appendPlainText("本机IP地址:"+aHost.toString()); //IP地址

        ui->plainTextEdit->appendPlainText("");

        }

    }

}

void Dialog::on_btnDetail_clicked()

{

    QList<QHostAddress> addList=QNetworkInterface::allAddresses();//

    if (!addList.isEmpty())

    for (int i=0;i<addList.count();i++)

    {

        QHostAddress aHost=addList.at(i);

        bool show=ui->chkOnlyIPv4->isChecked();//只显示IPv4

        if (show)

            show=QAbstractSocket::IPv4Protocol==aHost.protocol();

        else

            show=true;

        if (show)

        {

        ui->plainTextEdit->appendPlainText("协  议:"+protocolName(aHost.protocol()));

        ui->plainTextEdit->appendPlainText("IP地址:"+aHost.toString());

        ui->plainTextEdit->appendPlainText("");

        }

    }

}

void Dialog::on_btnLookup_clicked()

{//查找主机信息

    QString hostname=ui->editHost->text(); //主机名

    ui->plainTextEdit->appendPlainText("正在查找查找主机信息:"+hostname);

    QHostInfo::lookupHost(hostname,this,SLOT(lookedUpHostInfo(QHostInfo)));

}

void Dialog::on_btnALLInterface_clicked()

{//QNetworkInterface::allInterfaces()函数的使用

    QList<QNetworkInterface>    list=QNetworkInterface::allInterfaces();

    for(int i=0;i<list.count();i++)

    {

        QNetworkInterface aInterface=list.at(i);

        if (!aInterface.isValid())

           continue;

        ui->plainTextEdit->appendPlainText("设备名称:"+aInterface.humanReadableName());

        ui->plainTextEdit->appendPlainText("硬件地址:"+aInterface.hardwareAddress());

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

        for(int j=0;j<entryList.count();j++)

        {

            QNetworkAddressEntry aEntry=entryList.at(j);

            ui->plainTextEdit->appendPlainText("   IP 地址:"+aEntry.ip().toString());

            ui->plainTextEdit->appendPlainText("   子网掩码:"+aEntry.netmask().toString());

            ui->plainTextEdit->appendPlainText("   广播地址:"+aEntry.broadcast().toString()+"\n");

        }

        ui->plainTextEdit->appendPlainText("\n");

    }

}

QString Dialog::protocolName(QAbstractSocket::NetworkLayerProtocol protocol)

{//通过协议类型返回协议名称

  switch(protocol)

  {

    case QAbstractSocket::IPv4Protocol:

        return "IPv4 Protocol";

    case QAbstractSocket::IPv6Protocol:

      return "IPv6 Protocol";

    case QAbstractSocket::AnyIPProtocol:

      return "Any IP Protocol";

    default:

      return "Unknown Network Layer Protocol";

   }

}

void Dialog::on_btnClear_clicked()

{

    ui->plainTextEdit->clear();

}

三.测试结果

1.获取本机主机名和IP

QT网络编程之获取本机网络信息-LMLPHP

2.获取所有IP地址

QT网络编程之获取本机网络信息-LMLPHP

3.获取所有接口

QT网络编程之获取本机网络信息-LMLPHP

4.查找域名的ip地址

QT网络编程之获取本机网络信息-LMLPHP

03-15 13:36