feat: 使用QHostInfo来获取域名的服务器IP
This commit is contained in:
parent
2160977193
commit
04e49653a0
89
QtQHostInfoEx/ExQHostInfo.cpp
Executable file
89
QtQHostInfoEx/ExQHostInfo.cpp
Executable file
@ -0,0 +1,89 @@
|
||||
#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";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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<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
|
||||
|
||||
if (bIpv4) {
|
||||
bIpv4 = QAbstractSocket::IPv4Protocol == host.protocol();
|
||||
} else {
|
||||
bIpv4 = false;
|
||||
}
|
||||
|
||||
if (bIpv4) {
|
||||
ui->plainTextEdit->appendPlainText("协议:" + protocolName(host.protocol()));
|
||||
ui->plainTextEdit->appendPlainText(host.toString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
41
QtQHostInfoEx/ExQHostInfo.h
Executable file
41
QtQHostInfoEx/ExQHostInfo.h
Executable file
@ -0,0 +1,41 @@
|
||||
#ifndef EXQHOSTINFO_H
|
||||
#define EXQHOSTINFO_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QAbstractSocket>
|
||||
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
|
87
QtQHostInfoEx/ExQHostInfo.ui
Executable file
87
QtQHostInfoEx/ExQHostInfo.ui
Executable file
@ -0,0 +1,87 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>ExQHostInfo</class>
|
||||
<widget class="QWidget" name="ExQHostInfo">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>635</width>
|
||||
<height>320</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>ExQHostInfo</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="2" column="0" colspan="2">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout"/>
|
||||
</item>
|
||||
<item row="4" column="0" colspan="2">
|
||||
<widget class="QLineEdit" name="lineEdit">
|
||||
<property name="text">
|
||||
<string>www.baidu.com</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="2">
|
||||
<widget class="QPushButton" name="btnFindIP">
|
||||
<property name="text">
|
||||
<string>QHostInfo查询左侧域名IP地址</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="6" column="0" colspan="3">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2"/>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="QPushButton" name="btnAllInterfaces">
|
||||
<property name="text">
|
||||
<string>QNetworkInterface::allInterfaces()</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QPushButton" name="btnGetHostInfo">
|
||||
<property name="text">
|
||||
<string>QHostInfo查询主机名和IP</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QPushButton" name="btnAllAddresses">
|
||||
<property name="text">
|
||||
<string>QNetworkInterface::allAddresses()</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="0">
|
||||
<widget class="QCheckBox" name="checkBox">
|
||||
<property name="text">
|
||||
<string>只显示IPv4协议的网络信息</string>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="2">
|
||||
<widget class="QPushButton" name="btnClean">
|
||||
<property name="text">
|
||||
<string>清空</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPlainTextEdit" name="plainTextEdit"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
BIN
QtQHostInfoEx/Icon.icns
Executable file
BIN
QtQHostInfoEx/Icon.icns
Executable file
Binary file not shown.
51
QtQHostInfoEx/QtQHostInfoEx.pro
Executable file
51
QtQHostInfoEx/QtQHostInfoEx.pro
Executable file
@ -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
|
BIN
QtQHostInfoEx/icon.ico
Executable file
BIN
QtQHostInfoEx/icon.ico
Executable file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
11
QtQHostInfoEx/main.cpp
Executable file
11
QtQHostInfoEx/main.cpp
Executable file
@ -0,0 +1,11 @@
|
||||
#include "ExQHostInfo.h"
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
ExQHostInfo w;
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
Loading…
Reference in New Issue
Block a user