2019-11-19 15:59:39 +08:00
|
|
|
|
#include "ExQHostInfo.h"
|
|
|
|
|
#include "ui_ExQHostInfo.h"
|
|
|
|
|
|
|
|
|
|
#include <QHostInfo>
|
|
|
|
|
#include <QNetworkInterface>
|
|
|
|
|
|
|
|
|
|
ExQHostInfo::ExQHostInfo(QWidget *parent) :
|
|
|
|
|
QWidget(parent),
|
|
|
|
|
ui(new Ui::ExQHostInfo)
|
|
|
|
|
{
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
|
|
setWindowTitle("QHostInfo/QNetworkInterface查询主机网络状态:");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExQHostInfo::~ExQHostInfo()
|
|
|
|
|
{
|
|
|
|
|
delete ui;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//通过协议类型返回协议名称
|
|
|
|
|
QString ExQHostInfo::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 "Unknow Network Layer Protocol";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-19 23:21:26 +08:00
|
|
|
|
//QHostInfo 获取主机信息
|
2019-11-19 15:59:39 +08:00
|
|
|
|
void ExQHostInfo::on_btnGetHostInfo_clicked()
|
|
|
|
|
{
|
2019-11-19 23:21:26 +08:00
|
|
|
|
QString hostName = QHostInfo::localHostName();
|
|
|
|
|
ui->plainTextEdit->appendPlainText("本地主机名称:" + hostName + "\n");
|
|
|
|
|
|
|
|
|
|
QHostInfo hostInfo = QHostInfo::fromName(hostName);
|
|
|
|
|
QList<QHostAddress> list = hostInfo.addresses();
|
|
|
|
|
|
|
|
|
|
if (list.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (QHostAddress var, list) {
|
|
|
|
|
bool bIPv4 = ui->checkBox->isChecked();
|
2019-11-19 15:59:39 +08:00
|
|
|
|
|
2019-11-19 23:21:26 +08:00
|
|
|
|
if (bIPv4) { //只显示 IPv4
|
|
|
|
|
bIPv4 = QAbstractSocket::IPv4Protocol == var.protocol();
|
|
|
|
|
} else {
|
|
|
|
|
bIPv4 = true; //显示 IPv4 和 IPv6
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bIPv4) {
|
|
|
|
|
ui->plainTextEdit->appendPlainText("协议:" + protocolName(var.protocol()));
|
|
|
|
|
ui->plainTextEdit->appendPlainText("本机IP地址" + var.toString() + "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-19 15:59:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExQHostInfo::on_btnAllAddresses_clicked()
|
|
|
|
|
{
|
2019-11-19 23:21:26 +08:00
|
|
|
|
QList<QHostAddress> list = QNetworkInterface::allAddresses();
|
|
|
|
|
|
|
|
|
|
if (list.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (QHostAddress var, list) {
|
|
|
|
|
bool bIPv4 = ui->checkBox->isChecked();
|
2019-11-19 15:59:39 +08:00
|
|
|
|
|
2019-11-19 23:21:26 +08:00
|
|
|
|
if (bIPv4) { //只显示 IPv4
|
|
|
|
|
bIPv4 = QAbstractSocket::IPv4Protocol == var.protocol();
|
|
|
|
|
} else {
|
|
|
|
|
bIPv4 = true; //显示 IPv4 和 IPv6
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bIPv4) {
|
|
|
|
|
ui->plainTextEdit->appendPlainText("协议:" + protocolName(var.protocol()));
|
|
|
|
|
ui->plainTextEdit->appendPlainText("本机IP地址" + var.toString() + "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-19 15:59:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExQHostInfo::on_btnAllInterfaces_clicked()
|
|
|
|
|
{
|
2019-11-19 23:21:26 +08:00
|
|
|
|
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
|
|
|
|
|
|
|
|
|
|
if (list.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (QNetworkInterface var, list) {
|
|
|
|
|
if (!var.isValid())
|
|
|
|
|
continue;
|
2019-11-19 15:59:39 +08:00
|
|
|
|
|
2019-11-19 23:21:26 +08:00
|
|
|
|
ui->plainTextEdit->appendPlainText("设备名称:" + var.humanReadableName());
|
|
|
|
|
ui->plainTextEdit->appendPlainText("硬件地址:" + var.hardwareAddress());
|
|
|
|
|
|
|
|
|
|
QList<QNetworkAddressEntry> entry = var.addressEntries();
|
|
|
|
|
foreach (QNetworkAddressEntry ent, entry) {
|
|
|
|
|
ui->plainTextEdit->appendPlainText(" IP 地址:" + ent.ip().toString());
|
|
|
|
|
ui->plainTextEdit->appendPlainText(" 子网掩码:" + ent.netmask().toString());
|
|
|
|
|
ui->plainTextEdit->appendPlainText(" 子网广播:" + ent.broadcast().toString() + "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-19 15:59:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExQHostInfo::on_btnFindIP_clicked()
|
|
|
|
|
{
|
|
|
|
|
QString hostName = ui->lineEdit->text(); //域名
|
|
|
|
|
ui->plainTextEdit->appendPlainText("正在查找域名的服务器的主机信息:" + hostName);
|
|
|
|
|
QHostInfo::lookupHost(hostName, this, SLOT(onLookedUpHostInfo(QHostInfo)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExQHostInfo::on_btnClean_clicked()
|
|
|
|
|
{
|
|
|
|
|
ui->plainTextEdit->clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//查询主机信息的槽函数
|
|
|
|
|
void ExQHostInfo::onLookedUpHostInfo(const QHostInfo &host)
|
|
|
|
|
{
|
|
|
|
|
QList<QHostAddress> list = host.addresses();
|
|
|
|
|
|
|
|
|
|
if (list.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < list.count(); i++) {
|
|
|
|
|
QHostAddress host = list.at(i);
|
|
|
|
|
bool bIpv4 = ui->checkBox->isChecked(); //只显示IPv4
|
|
|
|
|
|
2019-11-19 23:21:26 +08:00
|
|
|
|
if (bIpv4) { //只显示 IPv4
|
2019-11-19 15:59:39 +08:00
|
|
|
|
bIpv4 = QAbstractSocket::IPv4Protocol == host.protocol();
|
|
|
|
|
} else {
|
2019-11-19 23:21:26 +08:00
|
|
|
|
bIpv4 = true; //显示 IPv4 和 IPv6
|
2019-11-19 15:59:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bIpv4) {
|
|
|
|
|
ui->plainTextEdit->appendPlainText("协议:" + protocolName(host.protocol()));
|
|
|
|
|
ui->plainTextEdit->appendPlainText(host.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|