From 04e49653a0b94d03175274898a5aa8383aa295d9 Mon Sep 17 00:00:00 2001 From: yuanyi Date: Tue, 19 Nov 2019 15:59:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8QHostInfo=E6=9D=A5?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=9F=9F=E5=90=8D=E7=9A=84=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8IP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QtQHostInfoEx/ExQHostInfo.cpp | 89 ++++++++++++++++++++++++++++++++ QtQHostInfoEx/ExQHostInfo.h | 41 +++++++++++++++ QtQHostInfoEx/ExQHostInfo.ui | 87 +++++++++++++++++++++++++++++++ QtQHostInfoEx/Icon.icns | Bin 0 -> 5566 bytes QtQHostInfoEx/QtQHostInfoEx.pro | 51 ++++++++++++++++++ QtQHostInfoEx/icon.ico | Bin 0 -> 4286 bytes QtQHostInfoEx/main.cpp | 11 ++++ 7 files changed, 279 insertions(+) create mode 100755 QtQHostInfoEx/ExQHostInfo.cpp create mode 100755 QtQHostInfoEx/ExQHostInfo.h create mode 100755 QtQHostInfoEx/ExQHostInfo.ui create mode 100755 QtQHostInfoEx/Icon.icns create mode 100755 QtQHostInfoEx/QtQHostInfoEx.pro create mode 100755 QtQHostInfoEx/icon.ico create mode 100755 QtQHostInfoEx/main.cpp diff --git a/QtQHostInfoEx/ExQHostInfo.cpp b/QtQHostInfoEx/ExQHostInfo.cpp new file mode 100755 index 0000000..4bcf7b6 --- /dev/null +++ b/QtQHostInfoEx/ExQHostInfo.cpp @@ -0,0 +1,89 @@ +#include "ExQHostInfo.h" +#include "ui_ExQHostInfo.h" + +#include +#include + +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"; + } +} + + +void ExQHostInfo::on_btnGetHostInfo_clicked() +{ + +} + +void ExQHostInfo::on_btnAllAddresses_clicked() +{ + +} + +void ExQHostInfo::on_btnAllInterfaces_clicked() +{ + +} + +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 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 + + if (bIpv4) { + bIpv4 = QAbstractSocket::IPv4Protocol == host.protocol(); + } else { + bIpv4 = false; + } + + if (bIpv4) { + ui->plainTextEdit->appendPlainText("协议:" + protocolName(host.protocol())); + ui->plainTextEdit->appendPlainText(host.toString()); + } + + } +} + diff --git a/QtQHostInfoEx/ExQHostInfo.h b/QtQHostInfoEx/ExQHostInfo.h new file mode 100755 index 0000000..ecd4705 --- /dev/null +++ b/QtQHostInfoEx/ExQHostInfo.h @@ -0,0 +1,41 @@ +#ifndef EXQHOSTINFO_H +#define EXQHOSTINFO_H + +#include +#include +class QHostInfo; + + +namespace Ui { +class ExQHostInfo; +} + +class ExQHostInfo : public QWidget +{ + Q_OBJECT + +public: + explicit ExQHostInfo(QWidget *parent = nullptr); + ~ExQHostInfo(); + +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); + + + + +private: + Ui::ExQHostInfo *ui; + +}; + +#endif // EXQHOSTINFO_H diff --git a/QtQHostInfoEx/ExQHostInfo.ui b/QtQHostInfoEx/ExQHostInfo.ui new file mode 100755 index 0000000..6c7d980 --- /dev/null +++ b/QtQHostInfoEx/ExQHostInfo.ui @@ -0,0 +1,87 @@ + + + ExQHostInfo + + + + 0 + 0 + 635 + 320 + + + + ExQHostInfo + + + + + + + + + + + www.baidu.com + + + + + + + QHostInfo查询左侧域名IP地址 + + + + + + + + + + QNetworkInterface::allInterfaces() + + + + + + + QHostInfo查询主机名和IP + + + + + + + QNetworkInterface::allAddresses() + + + + + + + 只显示IPv4协议的网络信息 + + + true + + + + + + + 清空 + + + + + + + + + + + + + + diff --git a/QtQHostInfoEx/Icon.icns b/QtQHostInfoEx/Icon.icns new file mode 100755 index 0000000000000000000000000000000000000000..162a9a29baffd41ac9ab07adccbadcf0108de63b GIT binary patch literal 5566 zcmY+H2UHZ-*T6xs7pz1P6%Cq1QPE&xjS(vvF$mZd6p(qR+k|PC`5g*s3qkcu9k$f1sP4TDlLghUC4OM5;B^+HNWCzQ#Q%H0C~7eAC-iiYm6G!3wH(-B&;oe*_#lxD3~1DRp{yV;HT^pJW4Ay7!6pOB z6bGBlI!VuPPcLMIHBxON4vkLA%I~7cqymi*i3f6;scm!)O9z`^gXUUJ4m#ywM#m^e zBA)nQo!)Gg_Yg;v9JbO3n~bSx*!0x6R7^z=0~ESHPF{hbI-FaB&4Gfw$cDMGy4{ zd%k5#1pOqjltdA6AiiicB@>Io;&7Lv&j0C}tA4v>YXlKubyG#0L7^1n^ToZ-(z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LY&XOivIJ2pfxUb^bOs!@7xTC^hP_oKI0t}GwjkZ)mC?RXRed56%)!*@kA z_f?1`J_mxTv+_5`lrZ=Kz%mVLkv@D|%IZgBxlvUOHTPJ%JO-~~itO8wL0 zj+58hvF4u0H;SG`(GBh8v{MY?B8jO&M{)U}cK% z`HLcExX&*-c;o$w^Xd2E^Mo5i=(Pl|rO$QpAGCy0Y(#qKx_uXHcCoG)D1p$=mFe<1 zr(E|$X_t@mdM=%NgKov#8Rh8O)u630>qg$xgqRopey;wA!$a20ljz8EBZJmvBc3xY z^N$0=UK`vN5Vml3f#?cs!T7A7WX&Y^9zSx`-UAzZ=%hr)O zZ9jvZ>cace4%xIf{qUpHip=&yx%d22Jw7OdHxYBw%Wx4bKIie%|Mc408vJF!MH~K= zeBCD%y*F+=io1#Z)?Z#6{cg|Bks-;7=xsi;tM}?v39k>#RuUAq zHno%Ucu&E*ycd;=i=^I-@XKo}H)(eip2Ph@d{T|YUabstUZzd+T-Ozid4R4SU+>Cf z8FTOHIV;bWepGi3C(^!OQeIFphnzm!vp~A%b`LhCa*Em7|2O(xI?R6Ig1Hx`9zRqg zbpwb6mBOj71DurBnj~l3fo6vphwMx0E2_?-xZWo2p{(m~Mug|Y$-;W~FdOXFt3l7t zex&Xu?}^ekuO+Twe)zS3!63)(c&wh5_<{B*($seOx4U6s(eK}(~; z`aF5V;#hNSm~~9x)X)n_^l-D!%0oNomm@B$q#I)gEZt`RAz9$jAg@_`9r literal 0 HcmV?d00001 diff --git a/QtQHostInfoEx/QtQHostInfoEx.pro b/QtQHostInfoEx/QtQHostInfoEx.pro new file mode 100755 index 0000000..deca4a7 --- /dev/null +++ b/QtQHostInfoEx/QtQHostInfoEx.pro @@ -0,0 +1,51 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-11-18T19:47:03 +# +#------------------------------------------------- + +QT += core gui network + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtQHostInfoEx +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + ExQHostInfo.cpp + +HEADERS += \ + ExQHostInfo.h + +FORMS += \ + ExQHostInfo.ui + + +macx { +ICON = Icon.icns +} +unix:!macx{ +# linux only +} +win32 { +RC_ICONS = icon.ico +} + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/QtQHostInfoEx/icon.ico b/QtQHostInfoEx/icon.ico new file mode 100755 index 0000000000000000000000000000000000000000..7213a9dace8875b2fdba3a0a834187695a174c03 GIT binary patch literal 4286 zcmc&&>vL4~8BYI!PCxat-)d(%9jDXLI@W4O#a650tEIOptn-sgRu z_kDjuMa7T!=dQad#NQQXe^OEL(~63UyLs|f#lt+seR=TN=#NWh=|)9G-*5oEJclPd z7`S0X|CI%tJJ*k8_{b(0!9Yu?5yyPn6sT|3ao=kILUjK4np z1h%}e5wAY?41Ry_udwB%=do?`OK562gRd?dv3vh%Y<;s52M@L4$cOKty{!$2)G7*v zJS6^5028w=3|!Tsw?~7PW))P`hfCNE4N%urqqXe>4pr{L-(KH<>Vx}nXzw24J&mm| zK8H6qZ^COYZp2^y^anipz8z`b~T>+87CV?kTj3_ksGp6e_Q)qjUm?PoDHK8sww z2$NNj_t2I6h8lU3F8wsL}Pil zJz|hwI}CthON9R2^t&Y(i606I3cSRAOcB+m3&Vz1z28&p+Np<-c}Io?FTH z&6i(BXU8C_4m9B9jZfjlCmzFnzxWyU@7WFQQV3_xFQAqD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3?U*te=R+KQqE6N(lZ@Iu953E@gbNiWo$FBsed` zA@1c_BmO`bMPA1fD>4r*cMM79M(C1bUl)Baoz6*~!C;u*5E_J7dloD&JLJE`_wuXf36rFcaRi`kM+r(}MGR;#>c zT6VMFI_}I|p{3EE!XnQE^OR?9P3(_tjA>#^5gi*E{UF~%V3!sk^mqcOtE zn#|Cv1?F9F6u5~*0%6u%I$f%t_1`I9i@jGUx+2dkxr;qm`PqM4MGS&VHp@L1HE=oo lFfQ3)vv5z!IUS9}WQXnlmvdMCe}Mnj`X9zA(Ix-@ literal 0 HcmV?d00001 diff --git a/QtQHostInfoEx/main.cpp b/QtQHostInfoEx/main.cpp new file mode 100755 index 0000000..c88c043 --- /dev/null +++ b/QtQHostInfoEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExQHostInfo.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExQHostInfo w; + w.show(); + + return a.exec(); +}