一、HostInfo 简介
QHostInfo 是 Qt 网络模块中的一个类,用于进行主机名和 IP 地址之间的解析(DNS 解析)。它可以将主机名解析为 IP 地址,也可以将 IP 地址反解析为主机名。
头文件:
#include <QHostInfo>
所属模块:
QT += network
作用:
- 正向解析(Forward Lookup):主机名 → IP 地址;
- 反向解析(Reverse Lookup):IP 地址 → 主机名。
二、常用成员函数

三、用法示例
3.1 异步正向解析主机名(如 www.example.com)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QPlainTextEdit>
#include <QHostInfo>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr) : QWidget(parent) {
this->setFixedSize(800, 600);
this->setWindowTitle("查询域名或IP地址---测试程序");
m_pLineEdit = new QLineEdit(this);
m_pLineEdit->setText("www.126.com");
m_pBtn1 = new QPushButton("查询域名或IP地址", this);
connect(m_pBtn1, &QPushButton::clicked, this, &MainWindow::OnBtnGetDomainIPClicked);
m_pBtn2 = new QPushButton("清除数据信息", this);
connect(m_pBtn2, &QPushButton::clicked, this, &MainWindow::OnBtnClearDataClicked);
m_pTextEdit = new QPlainTextEdit(this);
QHBoxLayout *pHBoxLayout = new QHBoxLayout();
pHBoxLayout->addWidget(m_pLineEdit);
pHBoxLayout->addWidget(m_pBtn1);
pHBoxLayout->addWidget(m_pBtn2);
QVBoxLayout *pVBoxLayout = new

最低0.47元/天 解锁文章
675

被折叠的 条评论
为什么被折叠?



