QHostInfo Class
The QHostInfo class provides host name lookup. More…
| Header | #include <QHostInfo> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Network) |
| Module | Nt Network C++ Classes |
List of all members, including inherited members
Public Types
| enum | HostInfoError |
Public Functions
| QHostInfo(int lookupId = -1) | |
| QString | hostName() const |
| void | setHostName(const QString &name) |
| QList<QHostAddress> | addresses() const |
| void | setAddresses(const QList<QHostAddress> &addresses) |
| HostInfoError | error() const |
| void | setError(HostInfoError error) |
| QString | errorString() const |
| void | setErrorString(const QString &errorString) |
| int | lookupId() const |
| void | setLookupId(int id) |
Static Public Members
| QHostInfo | fromName(const QString &name) |
| void | abortHostLookup(int lookupId) |
| QString | localHostName() |
| QString | localDomainName() |
| int | lookupHost(const QString &name, Func &&functor) |
| int | lookupHost(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.
| Constant | Description |
|---|---|
QHostInfo::NoError | The lookup succeeded. |
QHostInfo::HostNotFound | No addresses were found for the name. |
QHostInfo::UnknownError | Another error occurred. |
Member Function Documentation
QHostInfo::QHostInfo(int lookupId = -1)
Constructs an empty result with the lookup ID lookupId.
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().
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().
QString QHostInfo::localDomainName()
Returns this machine's DNS domain, or an empty string if it has none.
See also localHostName().
QString QHostInfo::localHostName()
Returns this machine's host name.
See also localDomainName().
template <typename Func> 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> 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().