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 0000000..162a9a2 Binary files /dev/null and b/QtQHostInfoEx/Icon.icns differ 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 0000000..7213a9d Binary files /dev/null and b/QtQHostInfoEx/icon.ico differ 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(); +}