6.0.0-rc4Source

QHostInfo Class

The QHostInfo class provides host name lookup. More…

Header#include <QHostInfo>
CMaketarget_link_libraries(app PRIVATE Nt::Network)
ModuleNt Network C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QHostInfo(int lookupId = -1)
QStringhostName() const
voidsetHostName(const QString &name)
QList<QHostAddress>addresses() const
voidsetAddresses(const QList<QHostAddress> &addresses)
HostInfoErrorerror() const
voidsetError(HostInfoError error)
QStringerrorString() const
voidsetErrorString(const QString &errorString)
intlookupId() const
voidsetLookupId(int id)

Static Public Members

QHostInfofromName(const QString &name)
voidabortHostLookup(int lookupId)
QStringlocalHostName()
QStringlocalDomainName()
intlookupHost(const QString &name, Func &&functor)
intlookupHost(const QString &name, const QObject *context, Func &&functor)

Detailed Description

Call lookupHost() to resolve a name without blocking the event loop. The function you pass receives the result:

QHostInfo::lookupHost("example.com", [](const QHostInfo &info) {
    if (info.error() == QHostInfo::NoError)
        qDebug() << info.addresses().first().toString();
});

fromName() does the same lookup and blocks until it finishes.

See also QHostAddress.

Differences from Qt

There is no string-based SLOT() overload of lookupHost(). Each lookup uses its own thread rather than a shared pool, and abortHostLookup() only discards the result: the system resolver call itself runs to completion.

On Zephyr a lookup needs CONFIG_DNS_RESOLVER and runs in a thread with a stack of CONFIG_NT_THREAD_STACK_SIZE bytes; if that thread cannot be started, lookupHost() resolves the name before it returns. See Nt on Zephyr.

Member Type Documentation

enum QHostInfo::HostInfoError

Describes the outcome of a lookup.

ConstantDescription
QHostInfo::NoErrorThe lookup succeeded.
QHostInfo::HostNotFoundNo addresses were found for the name.
QHostInfo::UnknownErrorAnother error occurred.

Member Function Documentation

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

Constructs an empty result with the lookup ID lookupId.

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

Discards the result of the lookup lookupId, if it has not been delivered.

See also lookupHost().

QList<QHostAddress> QHostInfo::addresses() const

Returns the addresses found for the host.

See also setAddresses().

HostInfoError QHostInfo::error() const

Returns the error of the lookup, or NoError if it succeeded.

See also errorString().

QString QHostInfo::errorString() const

Returns a description of the last error.

See also error().

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

Looks up name and blocks until the result is known. An IP address is reverse-resolved; if that fails, its text is used as the host name.

See also lookupHost().

QString QHostInfo::hostName() const

Returns the name that was looked up.

See also setHostName().

[static] QString QHostInfo::localDomainName()

Returns this machine's DNS domain, or an empty string if it has none.

See also localHostName().

[static] QString QHostInfo::localHostName()

Returns this machine's host name.

See also localDomainName().

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

Looks up name on a background thread and calls functor with the result from the event loop. Returns the lookup's ID, which abortHostLookup() accepts.

See also fromName().

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

This is an overloaded function.

Looks up name and calls functor, unless context is destroyed first.

int QHostInfo::lookupId() const

Returns the ID of this lookup, or -1 if there is none.

See also setLookupId().

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

Sets the list of found addresses to addresses.

See also addresses().

void QHostInfo::setError(HostInfoError error)

Sets the error to error.

See also error().

void QHostInfo::setErrorString(const QString &errorString)

Sets the error description to errorString.

See also errorString().

void QHostInfo::setHostName(const QString &name)

Sets the host name to name.

See also hostName().

void QHostInfo::setLookupId(int id)

Sets the lookup ID to id.

See also lookupId().