From 8ac140b9838fca6b0ccd8d0f04c4076e34f05961 Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Tue, 19 Nov 2019 23:21:26 +0800 Subject: [PATCH] feat: QHostInfo and QNetworkInterface --- .DS_Store | Bin 0 -> 10244 bytes QtQHostInfoEx/ExQHostInfo.cpp | 65 +++++++++++++++++++++++++++++++--- QtQHostInfoEx/ExQHostInfo.h | 15 ++++---- 3 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..bba77c3f70b46699f96fcc32e1b37e63c0381afb GIT binary patch literal 10244 zcmeHMTWl0n82^kGgi^Pf5Y z`Oi83H{Ut`oHGD`fmE~{zzYBZT}(d~bF$~on=i1GD;x?9 z>cb&ZHd97GP99`Wdu4MlZ0Fl_JvNb-!yT$J+$R+_>Y6F5nv%4P)u=*MdOB4tst*VB zlomCtc0ybsveYLPjg6K2Ykg(jn#vPpUOQi2QCV@~geWX2E#DG693MS4cKpOcXSjE0 zuMm)z;qBR}Tva1O3GOPMO1pYJ?W*Wnkdu2)o|KCQMb=j1DDfknEO|Nm(0E0;7jwa#)GA zY60Ce^>~*`BV?npnhPc5h^h^gY`ejs5=-h=@tg!DY4ijM+cY_@ka9aOE2-aYD+MTp zP0#`za0HIQX*dV3!CPDmD4m|Y(1qf@3vNP$68hd6{tjzSC+XoeAX`ed{B=`71P;!RKkv*-4nNsT8C@ z&`Ir`qD#5YUqzFs@}EtjG&)oG95aR*`NEO)e0qv}g?8AZSl|xsa_yON zxt+ZHcSiJBDy|uQQhr2^MN%=@R2pJ2w)7ekq2Yj`Crw)JsbZfbr3_`iLiN-krHN`A z6Kl1E;)R?=o)!16^;K=$)^v@9Bv*l}uyaT?)No8`A6BF0V7F{U6fH{Y`2h-`nyL1y zN>a?nxM(fk*A_2XD!Gf?%cipuq!OVk6;_X?RCAQ~QS81?l-?A2qBNWqJ<3{)S{iq; zyHpSkvhv2HLU&&5TIF6NO0v*qkej@Tb*>GfWQc7?D9UqFzpGM^qGG!hmv~W)t4@%1 zi=9I}c=5u`uB}2omWf>vJsu~23|`#eZWN@;LNDukk8GH&2}PssE$@omI|QMdX&RzY zB`QpWvvdIS@dbqTf~VmOyb2fL4R{wmgv%7iK7p(74SWYb zz)$cq`~ttiA1I(mF)fGU*$OPdmFUAd+=!cT3kGmE?!gDK72EI-_TpibaR3u|6b&@- zIG(~s@Ns+spT)Cy4qw8T@pXI~-@!}x9=?yC;%9h`k8dJ6w%Fsf1%6AJnF(x1zO3G< z2<&Oz?%H{G2>dnwkSd_BsO2TA*7<8TH%z?PjCXk}tt=*Ss#p|CQ^b0qxAT%1;4Zq? zTwC;-{<3bxB!>n17hM;_yVreSJe&p zoj5ob^Qx6XQ~EdABZ^!0YVaW1KZ(*OT6VD{~B%borIbVtB+i0177|Ly(%pP(5Sga7~l literal 0 HcmV?d00001 diff --git a/QtQHostInfoEx/ExQHostInfo.cpp b/QtQHostInfoEx/ExQHostInfo.cpp index 4bcf7b6..79f5de8 100755 --- a/QtQHostInfoEx/ExQHostInfo.cpp +++ b/QtQHostInfoEx/ExQHostInfo.cpp @@ -33,20 +33,78 @@ QString ExQHostInfo::protocolName(QAbstractSocket::NetworkLayerProtocol protocol } } - +//QHostInfo 获取主机信息 void ExQHostInfo::on_btnGetHostInfo_clicked() { + QString hostName = QHostInfo::localHostName(); + ui->plainTextEdit->appendPlainText("本地主机名称:" + hostName + "\n"); + QHostInfo hostInfo = QHostInfo::fromName(hostName); + QList list = hostInfo.addresses(); + + if (list.isEmpty()) + return; + + foreach (QHostAddress var, list) { + bool bIPv4 = ui->checkBox->isChecked(); + + 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"); + } + } } void ExQHostInfo::on_btnAllAddresses_clicked() { + QList list = QNetworkInterface::allAddresses(); + if (list.isEmpty()) + return; + + foreach (QHostAddress var, list) { + bool bIPv4 = ui->checkBox->isChecked(); + + 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"); + } + } } void ExQHostInfo::on_btnAllInterfaces_clicked() { + QList list = QNetworkInterface::allInterfaces(); + if (list.isEmpty()) + return; + + foreach (QNetworkInterface var, list) { + if (!var.isValid()) + continue; + + ui->plainTextEdit->appendPlainText("设备名称:" + var.humanReadableName()); + ui->plainTextEdit->appendPlainText("硬件地址:" + var.hardwareAddress()); + + QList 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"); + } + } } void ExQHostInfo::on_btnFindIP_clicked() @@ -73,17 +131,16 @@ void ExQHostInfo::onLookedUpHostInfo(const QHostInfo &host) QHostAddress host = list.at(i); bool bIpv4 = ui->checkBox->isChecked(); //只显示IPv4 - if (bIpv4) { + if (bIpv4) { //只显示 IPv4 bIpv4 = QAbstractSocket::IPv4Protocol == host.protocol(); } else { - bIpv4 = false; + bIpv4 = true; //显示 IPv4 和 IPv6 } if (bIpv4) { ui->plainTextEdit->appendPlainText("协议:" + protocolName(host.protocol())); ui->plainTextEdit->appendPlainText(host.toString()); } - } } diff --git a/QtQHostInfoEx/ExQHostInfo.h b/QtQHostInfoEx/ExQHostInfo.h index ecd4705..25b6c88 100755 --- a/QtQHostInfoEx/ExQHostInfo.h +++ b/QtQHostInfoEx/ExQHostInfo.h @@ -22,16 +22,13 @@ private: QString protocolName(QAbstractSocket::NetworkLayerProtocol protocol); //通过协议类型返回协议名称 private slots: - void on_btnGetHostInfo_clicked(); - void on_btnAllAddresses_clicked(); - void on_btnAllInterfaces_clicked(); - void on_btnFindIP_clicked(); - void on_btnClean_clicked(); - - void onLookedUpHostInfo(const QHostInfo& host); - - + void on_btnGetHostInfo_clicked(); //QHostInfo查询主机名和IP + void on_btnAllAddresses_clicked(); //QNetworkInterface::allAddresses() + void on_btnAllInterfaces_clicked(); //QNetworkInterface::allInterfaces() + void on_btnFindIP_clicked(); //QHostInfo查询左侧域名IP地址 + void on_btnClean_clicked(); //清空文本框信息 + void onLookedUpHostInfo(const QHostInfo& host); //查询主机信息的槽函数 private: Ui::ExQHostInfo *ui;