效果图
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/9ee2d8c437c1430f87eb46ebf2160aac.gif#pic_center)
代码
.pro
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/02f4db7177a0454ca85fd05390a0de0f.png)
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAbstractSocket>
#include <QHostInfo>
#include <QNetworkInterface>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
QString protocolName(QAbstractSocket::NetworkLayerProtocol protocol);
QString interfaceType(QNetworkInterface::InterfaceType type);
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void do_lookedUpHostInfo(const QHostInfo &host);
void on_btnGetHostInfo_clicked();
void on_btnLookup_clicked();
void on_btnAllAddress_clicked();
void on_btnAllInterface_clicked();
private:
Ui::MainWindow *ui;
};
#endif
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHostInfo>
#include <QNetworkInterface>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
QString MainWindow::protocolName(QAbstractSocket::NetworkLayerProtocol protocol)
{
switch(protocol)
{
case QAbstractSocket::IPv4Protocol:
return "IPv4";
case QAbstractSocket::IPv6Protocol:
return "IPv6";
case QAbstractSocket::AnyIPProtocol:
return "Any Internet Protocol";
default:
return "Unknown Network Layer Protocol";
}
}
void MainWindow::on_btnGetHostInfo_clicked()
{
ui->textEdit->clear();
QString hostName=QHostInfo::localHostName();
ui->textEdit->appendPlainText("本机主机名:"+hostName+"\n");
QHostInfo hostInfo=QHostInfo::fromName(hostName);
QList<QHostAddress> addrList=hostInfo.addresses();
if (addrList.isEmpty())
return;
foreach (QHostAddress host, addrList)
{
bool show=ui->chkBox_OnlyIPv4->isChecked();
show= show? (host.protocol()==QAbstractSocket::IPv4Protocol):true;
if (show)
{
ui->textEdit->appendPlainText("协 议:"+protocolName(host.protocol()));
ui->textEdit->appendPlainText("本机IP地址:"+host.toString());
ui->textEdit->appendPlainText(QString("isGlobal()=%1\n").arg(host.isGlobal()));
}
}
}
void MainWindow::do_lookedUpHostInfo(const QHostInfo &host)
{
QList<QHostAddress> addrList=host.addresses();
if (addrList.isEmpty())
return;
foreach(QHostAddress host, addrList)
{
bool show=ui->chkBox_OnlyIPv4->isChecked();
show= show? (host.protocol()==QAbstractSocket::IPv4Protocol):true;
if (show)
{
ui->textEdit->appendPlainText("协 议:"+protocolName(host.protocol()));
ui->textEdit->appendPlainText(host.toString());
ui->textEdit->appendPlainText(QString("isGlobal()=%1\n").arg(host.isGlobal()));
}
}
}
void MainWindow::on_btnLookup_clicked()
{
ui->textEdit->clear();
QString hostname=ui->comboBox->currentText();
ui->textEdit->appendPlainText("正在查找查找主机信息:"+hostname);
QHostInfo::lookupHost(hostname,this,SLOT(do_lookedUpHostInfo(QHostInfo)));
}
void MainWindow::on_btnAllAddress_clicked()
{
ui->textEdit->clear();
QList<QHostAddress> addrList=QNetworkInterface::allAddresses();
if (addrList.isEmpty())
return;
foreach (QHostAddress host, addrList)
{
bool show=ui->chkBox_OnlyIPv4->isChecked();
show= show? (host.protocol()==QAbstractSocket::IPv4Protocol):true;
if (show)
{
ui->textEdit->appendPlainText("协 议:"+protocolName(host.protocol()));
ui->textEdit->appendPlainText("IP地址:"+host.toString());
ui->textEdit->appendPlainText(QString("isGlobal()=%1\n").arg(host.isGlobal()));
}
}
}
void MainWindow::on_btnAllInterface_clicked()
{
ui->textEdit->clear();
QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();
foreach(const auto& item,list)
{
if (!item.isValid())
continue;
ui->textEdit->appendPlainText("设备名称:"+item.humanReadableName());
ui->textEdit->appendPlainText("硬件地址:"+item.hardwareAddress());
ui->textEdit->appendPlainText("接口类型:"+interfaceType(item.type()));
QList<QNetworkAddressEntry> entryList=item.addressEntries();
foreach(const auto& entry, entryList)
{
ui->textEdit->appendPlainText(" IP 地址:"+entry.ip().toString());
ui->textEdit->appendPlainText(" 子网掩码:"+entry.netmask().toString());
ui->textEdit->appendPlainText(" 广播地址:"+entry.broadcast().toString()+"\n");
}
}
}
QString MainWindow::interfaceType(QNetworkInterface::InterfaceType type)
{
switch(type)
{
case QNetworkInterface::Unknown:
return "Unknown";
case QNetworkInterface::Loopback:
return "Loopback";
case QNetworkInterface::Ethernet:
return "Ethernet";
case QNetworkInterface::Wifi:
return "Wifi";
default:
return "Other type";
}
}