QHostInfo Class

QHostInfo 类为主机名查询提供静态函数。更多

Header: #include <QHostInfo>
CMake: find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network

注意:该类中的所有函数都是可重入的

公共类型

enum HostInfoError { NoError, HostNotFound, UnknownError }

公共函数

QHostInfo(int id = -1)
QHostInfo(const QHostInfo &other)
QHostInfo(QHostInfo &&other)
~QHostInfo()
QList<QHostAddress> addresses() const
QHostInfo::HostInfoError error() const
QString errorString() const
QString hostName() const
int lookupId() const
void setAddresses(const QList<QHostAddress> &addresses)
void setError(QHostInfo::HostInfoError error)
void setErrorString(const QString &str)
void setHostName(const QString &hostName)
void setLookupId(int id)
void swap(QHostInfo &other)
QHostInfo &operator=(QHostInfo &&other)
QHostInfo &operator=(const QHostInfo &other)

静态公共成员

void abortHostLookup(int id)
QHostInfo fromName(const QString &name)
QString localDomainName()
QString localHostName()
int lookupHost(const QString &name, const QObject *receiver, const char *member)
int lookupHost(const QString &name, Functor &&functor)
int lookupHost(const QString &name, const QObject *context, Functor functor)

详细说明

QHostInfo 可查找与主机名相关联的 IP 地址或与 IP 地址相关联的主机名。该类提供了两个静态便利函数:一个是异步函数,在找到主机后发出信号;另一个是阻塞函数,返回一个 QHostInfo 对象。

要异步查找主机的 IP 地址,请调用lookupHost() ,该函数将主机名或 IP 地址、接收器对象和槽签名作为参数,并返回一个 ID。您可以使用查询 ID 调用abortHostLookup() 终止查询。

示例

// To find the IP address of qt-project.org
QHostInfo::lookupHost("qt-project.org", this, &MyWidget::printResults);

// To find the host name for 4.2.2.1
QHostInfo::lookupHost("4.2.2.1", this, &MyWidget::printResults);

结果就绪时调用槽。结果存储在 QHostInfo 对象中。调用addresses() 获取主机的 IP 地址列表,调用hostName() 获取查询到的主机名。

如果查询失败,error() 会返回发生错误的类型。errorString() 会给出查询错误的可读描述。

如果需要阻塞查询,请使用QHostInfo::fromName() 函数:

QHostInfo info = QHostInfo::fromName("qt-project.org");

QHostInfo 通过 IDNA 和 Punycode 标准支持国际化域名(IDN)。

要检索本地主机的名称,请使用静态QHostInfo::localHostName() 函数。

QHostInfo 使用操作系统提供的机制来执行查询。根据RFC 6724,不能保证返回为某个域或主机注册的所有 IP 地址。

注: 自 Qt 4.6.1 起,QHostInfo 将使用多个线程进行 DNS 查询,而不是使用一个专门的 DNS 线程。这提高了性能,但与 Qt 以前的版本相比,也改变了使用lookupHost() 时的信号发射顺序。

注: 自 Qt 4.6.3 起,QHostInfo 将使用一个 60 秒的小型内部 DNS 缓存来提高性能。

另请参阅 QAbstractSocketRFC 3492RFC 6724

成员类型文档

enum QHostInfo::HostInfoError

该枚举描述了在尝试解析主机名时可能发生的各种错误。

常量说明
QHostInfo::NoError0查找成功。
QHostInfo::HostNotFound1未找到主机的 IP 地址。
QHostInfo::UnknownError2出现未知错误。

另请参阅 error() 和setError()。

成员函数文档

[explicit] QHostInfo::QHostInfo(int id = -1)

构造一个空的主机信息对象,其查找 ID 为id

另请参阅 lookupId() 。

QHostInfo::QHostInfo(const QHostInfo &other)

构造other 的副本。

[noexcept] QHostInfo::QHostInfo(QHostInfo &&other)

移动--从other 构建一个新的 QHostInfo。

注: 被移动对象other 处于部分形成状态,在这种状态下,唯一有效的操作是销毁和分配新值。

[noexcept] QHostInfo::~QHostInfo()

销毁主机信息对象。

[static] void QHostInfo::abortHostLookup(int id)

终止由lookupHost() 返回的 ID 为id 的主机查询。

另请参阅 lookupHost() 和lookupId()。

QList<QHostAddress> QHostInfo::addresses() const

返回与hostName() 相关联的 IP 地址列表。该列表可能为空。

示例:

QHostInfo info;
...
if (!info.addresses().isEmpty()) {
    QHostAddress address = info.addresses().first();
    // use the first IP address
}

另请参阅 setAddresses()、hostName() 和error()。

QHostInfo::HostInfoError QHostInfo::error() const

如果主机名查找失败,则返回发生错误的类型;否则返回NoError

另请参阅 setError() 和errorString()。

QString QHostInfo::errorString() const

如果查找失败,该函数将返回一个可读的错误描述;否则将返回 "未知错误"。

另请参阅 setErrorString() 和error()。

[static] QHostInfo QHostInfo::fromName(const QString &name)

查找给定主机的 IP 地址name 。在查询过程中,函数将被阻塞,这意味着程序的执行将暂停,直到查询结果出来为止。在QHostInfo 对象中返回查询结果。

如果向name 传递的是字面 IP 地址而不是主机名,QHostInfo 将搜索该 IP 的域名(即QHostInfo 将执行反向查询)。成功后,返回的QHostInfo 将同时包含解析的域名和主机名的 IP 地址。

另请参阅 lookupHost() 。

QString QHostInfo::hostName() const

返回查询到 IP 地址的主机名称。

另请参阅 setHostName() 和localHostName()。

[static] QString QHostInfo::localDomainName()

返回本机的 DNS 域。

注意: DNS 域与 Windows 网络中的域名无关。

另请参阅 hostName()。

[static] QString QHostInfo::localHostName()

如果配置了主机名,则返回该机器的主机名。请注意,主机名不保证全局唯一,尤其是自动配置的主机名。

此函数不保证返回的主机名是完全合格域名(FQDN)。为此,请使用fromName() 将返回的名称解析为 FQDN。

该函数的返回值与QSysInfo::machineHostName() 相同。

另请参阅 hostName() 和localDomainName()。

[static] int QHostInfo::lookupHost(const QString &name, const QObject *receiver, const char *member)

查询与主机名name 相关联的 IP 地址,并返回查询的 ID。当查询结果准备就绪时,receiver 中的槽或信号member 将被调用,参数为QHostInfo 。然后就可以检查QHostInfo 对象,获取查找结果。

查询只需调用一次函数即可完成,例如

QHostInfo::lookupHost("www.kde.org", this, &MyWidget::lookedUp);

槽的实现会打印查找返回地址的基本信息,如果查找失败则会报错:

voidMyWidget::lookedUp(constQHostInfo&host) {如果host.error()!=QHostInfo::NoError) {        qDebug() << "Lookup failed:" << host.errorString();
       return; }const autoaddresses=host.addresses();for(constQHostAddress&address: addresses)        qDebug() << "Found address:" << address.toString();
}

如果向name 传递的是字面 IP 地址而不是主机名,QHostInfo 将搜索该 IP 的域名(即QHostInfo 将执行反向查找)。成功后,生成的QHostInfo 将包含主机名的解析域名和 IP 地址。例如

QHostInfo::lookupHost("4.2.2.1", this, &MyWidget::lookedUp);

注意: 如果使用 lookupHost() 启动多个请求,则无法保证发出信号的顺序。

注意: 在 6.7 之前的 Qt 版本中,该函数将receiver 作为(非常数)QObject*

另请参阅 abortHostLookup()、addresses()、error() 和fromName()。

[static] template <typename Functor> int QHostInfo::lookupHost(const QString &name, Functor &&functor)

这是一个重载函数。

查找与主机名name 相关联的 IP 地址,并返回一个用于查找的 ID。当查询结果就绪时,将调用带有QHostInfo 参数的functor 。然后就可以检查QHostInfo 对象,获取查询结果。

functor 将在调用 lookupHost 的线程中运行;该线程必须有一个正在运行的 Qt 事件循环。

注意: 如果使用 lookupHost() 启动多个请求,则无法保证信号发出的顺序。

另请参阅 abortHostLookup()、addresses()、error() 和fromName()。

[static] template <typename Functor> int QHostInfo::lookupHost(const QString &name, const QObject *context, Functor functor)

这是一个重载函数。

查找与主机名name 相关联的 IP 地址,并返回一个用于查找的 ID。当查询结果就绪时,将调用带有QHostInfo 参数的functor 。然后就可以检查QHostInfo 对象,获取查询结果。

如果context 在查询完成前被销毁,则不会调用functorfunctor 将在context 的线程中运行。上下文的线程必须有一个正在运行的 Qt XML 事件循环。

下面是函数的另一种签名:

lookupHost(const QString &name, const QObject *receiver, PointerToMemberFunction function)

在这种情况下,当查找结果就绪时,receiver 中的槽或信号function 将被调用,参数为QHostInfo 。然后就可以检查QHostInfo 对象,获取查询结果。

注意: 如果使用 lookupHost() 启动多个请求,则无法保证信号发出的顺序。

另请参阅 abortHostLookup()、addresses()、error() 和fromName()。

int QHostInfo::lookupId() const

返回此查询的 ID。

另请参阅 setLookupId()、abortHostLookup() 和hostName()。

void QHostInfo::setAddresses(const QList<QHostAddress> &addresses)

QHostInfo 中的地址列表设置为addresses

另请参阅 addresses() 。

void QHostInfo::setError(QHostInfo::HostInfoError error)

QHostInfo 的错误类型设为error

另请参阅 error() 和errorString()。

void QHostInfo::setErrorString(const QString &str)

如果查找失败,则将发生错误的人可读描述设置为str

另请参阅 errorString() 和setError()。

void QHostInfo::setHostName(const QString &hostName)

QHostInfo 的主机名设置为hostName

另请参阅 hostName() 。

void QHostInfo::setLookupId(int id)

将此查询的 ID 设置为id

另请参阅 lookupId() 和lookupHost()。

[noexcept] void QHostInfo::swap(QHostInfo &other)

将此主机信息与other 互换。该操作速度非常快,从未出现过故障。

[noexcept] QHostInfo &QHostInfo::operator=(QHostInfo &&other)

移动--将other 赋值给此QHostInfo 实例。

注意: 从移动对象other 将处于部分形成状态,其中唯一有效的操作是销毁和赋新值。

QHostInfo &QHostInfo::operator=(const QHostInfo &other)

other 对象的数据赋值给主机信息对象,并返回该对象的引用。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.