QT6.5网络主机信息查询
立即下载
资源介绍:
QT6.5网络主机信息查询
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
//打印函数
void Widget::printResults(const QHostInfo &info)
{
QList address_list = info.addresses();;
foreach(auto address,address_list)
{
QString protocol = "协议:" + getProtocolName(address.protocol());
QString ip = "ip地址:" +address.toString();
QString globle ;
if(address.isGlobal())
globle = "是否全局地址:是";
else
globle = "是否全局地址:否";
ui->plainTextEdit->appendPlainText(protocol);
ui->plainTextEdit->appendPlainText(ip);
ui->plainTextEdit->appendPlainText(globle);
ui->plainTextEdit->appendPlainText("\n");
}
}
//QHostAddress使用
//返回协议名字
QString Widget::getProtocolName(int protocol)
{
switch (protocol) {
case QAbstractSocket::IPv4Protocol:
return QString("IPv4");
case QAbstractSocket::IPv6Protocol:
return QString("IPv6");
case QAbstractSocket::AnyIPProtocol:
return QString("Either IPv4 or IPv6");
case QAbstractSocket::UnknownNetworkLayerProtocol:
return QString("Other than IPv4 and IPv6");
default:
break;
}
return QString(" ");
}
void Widget::on_btn_local_host_name_ip_clicked()
{
ui->plainTextEdit->clear();
QString name = QHostInfo::localHostName();
QHostInfo info = QHostInfo::fromName(name);
ui->plainTextEdit->appendPlainText("本机主机名:" + name);
ui->plainTextEdit->appendPlainText("\n");
printResults(info);
}
void Widget::on_btn_network_host_name_ip_clicked()
{
ui->plainTextEdit->clear();
QString str = ui->lineEdit->text();
QHostInfo::lookupHost(str,this, &Widget::printResults);
}
//QNetworkInterface使用
//返回接口名字
QString Widget::interfaceType(int type)
{
switch (type) {
case QNetworkInterface::Unknown:
return QString("Unknown");
case QNetworkInterface::Loopback:
return QString("Loopback");
case QNetworkInterface::Virtual:
return QString("Virtual");
case QNetworkInterface::Ethernet:
return QString("Ethernet");
case QNetworkInterface::Slip:
return QString("Slip");
case QNetworkInterface::CanBus:
return QString("CanBus");
case QNetworkInterface::Ppp:
return QString("Ppp");
case QNetworkInterface::Fddi:
return QString("Fddi");
case QNetworkInterface::Wifi:
return QString("Wifi");
case QNetworkInterface::Phonet:
return QString("Phonet");
case QNetworkInterface::Ieee802154:
return QString("Ieee802154");
case QNetworkInterface::SixLoWPAN:
return QString("SixLoWPAN");
case QNetworkInterface::Ieee80216:
return QString("Ieee80216");
case QNetworkInterface::Ieee1394:
return QString("Ieee1394");
default:
break;
}
return QString(" ");
}
void Widget::on_btn_allAddresses_clicked()
{
ui->plainTextEdit->clear();
QList list = QNetworkInterface::allAddresses();
QHostInfo info;
info.setAddresses(list);
printResults(info);
}
void Widget::on_btn_allInterfaces_clicked()
{
ui->plainTextEdit->clear();
QList list = QNetworkInterface::allInterfaces();
foreach(auto interface,list)
{
ui->plainTextEdit->appendPlainText("start----------------------------------------------------------");
QString name = "设备底层名:" + interface.name();
QString readname = "设备可读名:" +interface.humanReadableName();
QString type = "接口类型:" + interfaceType(interface.type());
QString macAddress = "硬件(MAC)地址:" + interface.hardwareAddress();
QString maxValue = "最大传输单位:" + QString::number(interface.maximumTransmissionUnit());
ui->plainTextEdit->appendPlainText(name);
ui->plainTextEdit->appendPlainText(readname);
ui->plainTextEdit->appendPlainText(type);
ui->plainTextEdit->appendPlainText(macAddress);
ui->plainTextEdit->appendPlainText(maxValue);
QList addrList= interface.addressEntries();
foreach(auto addr,addrList)
{
QString ipaddr = "ip地址:" + addr.ip().toString();
QString mask = "子网掩码:" + addr.netmask().toString();
QString broad = "广播地址:" + addr.broadcast().toString();
ui->plainTextEdit->appendPlainText(ipaddr);
ui->plainTextEdit->appendPlainText(mask);
ui->plainTextEdit->appendPlainText(broad);
ui->plainTextEdit->appendPlainText("\r\n");
}
ui->plainTextEdit->appendPlainText("end----------------------------------------------------------");
ui->plainTextEdit->appendPlainText("\n");
}
}