protocol() QAbstractSocket::NetworkLayerProtocol类型变量,表示当前IP地址的协议类型 类型如下:
QAbstractSocket::IPv4Protocol
QAbstractSocket::IPv6Protocol
QAbstractSocket::AnyIPProtocol
QAbstractSocket::UnknowNetworkLayerProtocol
toString() 返回IP地址的字符串,表示程序中显示了IP地址列表中每个IP地址的协议类型和IP地址字符串
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAbstractSocket>
#include <QHostInfo>
#include <QNetworkInterface>
#include <QHostAddress>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void lookedUpHostInfo(const QHostInfo &info);
private:
Ui::MainWindow *ui;
QString protoclName(QAbstractSocket::NetworkLayerProtocol protocol);
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
//获取主机信息
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);
bool show = ui->checkBox->isChecked(); //只显示IPV4
if(show)
{
show = (QAbstractSocket::IPv4Protocol == aHost.protocol());
}else {
show = true;
}
if(show)
{
ui->plainTextEdit->appendPlainText("协议:"+protoclName(aHost.protocol()));
ui->plainTextEdit->appendPlainText("本机IP地址:"+aHost.toString());
ui->plainTextEdit->appendPlainText("");
}
}
}
}
void MainWindow::on_pushButton_2_clicked()
{
QList<QHostAddress> list = QNetworkInterface::allAddresses();
if(!list.isEmpty())
for(int i =0;i<list.count();i++)
{
QHostAddress aHost = list.at(i);
bool show = ui->checkBox->isChecked();
if(show)
show=QAbstractSocket::IPv4Protocol == aHost.protocol();
else
show =true;
if(show)
{
ui->plainTextEdit->appendPlainText("协议"+protoclName(aHost.protocol()));
ui->plainTextEdit->appendPlainText("IP地址"+aHost.toString());
ui->plainTextEdit->appendPlainText("");
}
}
}
void MainWindow::on_pushButton_3_clicked()
{
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> entry = aInterface.addressEntries();
for(int j = 0;j<entry.count();j++)
{
QNetworkAddressEntry aEntry = entry.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");
}
}
void MainWindow::on_pushButton_4_clicked()
{
QString hostname = ui->lineEdit->text(); //主机名
ui->plainTextEdit->appendPlainText("正在查找主机信息"+ hostname);
QHostInfo::lookupHost(hostname,this,SLOT(lookedUpHostInfo(QHostInfo)));
}
void MainWindow::on_pushButton_5_clicked()
{
ui->plainTextEdit->clear();
}
void MainWindow::lookedUpHostInfo(const QHostInfo & info)
{
QList<QHostAddress> addList = info.addresses();
if(!addList.isEmpty());
for(int i =0;i < addList.count(); i++)
{
QHostAddress aHost = addList.at(i);
bool show = ui->checkBox->isChecked();
if(show)
show = QAbstractSocket::IPv4Protocol ==aHost.protocol();
else
show = true;
if(show)
{
ui->plainTextEdit->appendPlainText("协议"+protoclName(aHost.protocol()));
ui->plainTextEdit->appendPlainText(aHost.toString());
}
}
}
//根据protocol()返回的QabstractSocket::NetworkLayerProtocol 枚举值显示协议名称字符串
QString MainWindow::protoclName(QAbstractSocket::NetworkLayerProtocol protocol)
{
switch (protocol)
{
case QAbstractSocket::IPv4Protocol:
return "IPV4 Protocol";
case QAbstractSocket::IPv6Protocol:
return "IPV4 Protocol";
case QAbstractSocket::AnyIPProtocol:
return "IPV4 Protocol";
default:
return "Unknow Network Layer Protocol";
}
}