QHostInfo 類

The QHostInfo class provides static functions for host name lookups. 更多...

頭: #include <QHostInfo>
qmake: QT += network

注意: 此類的所有函數 可重入 .

公共類型

enum HostInfoError { NoError, HostNotFound, UnknownError }

公共函數

QHostInfo (int id = -1)
QHostInfo (const 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= (const QHostInfo & other )
QHostInfo & operator= (QHostInfo && other )

靜態公共成員

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

詳細描述

The QHostInfo class provides static functions for host name lookups.

QHostInfo finds the IP address(es) associated with a host name, or the host name associated with an IP address. The class provides two static convenience functions: one that works asynchronously and emits a signal once the host is found, and one that blocks and returns a QHostInfo 對象。

要異步查找主機的 IP 地址,調用 lookupHost (),它以主機名或 IP 地址、接收者對象、及槽簽名作為自變量並返迴 ID。可以中止查找通過調用 abortHostLookup () 采用查找 ID。

範例:

// To find the IP address of qt-project.org
QHostInfo::lookupHost("qt-project.org",
                      this, SLOT(printResults(QHostInfo)));
// To find the host name for 4.2.2.1
QHostInfo::lookupHost("4.2.2.1",
                      this, SLOT(printResults(QHostInfo)));
					

The slot is invoked when the results are ready. The results are stored in a QHostInfo object. Call addresses () to get the list of IP addresses for the host, and hostName () to get the host name that was looked up.

If the lookup failed, error () returns the type of error that occurred. errorString () gives a human-readable description of the lookup error.

若想要阻塞查找,使用 QHostInfo::fromName () 函數:

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

QHostInfo supports Internationalized Domain Names (IDNs) through the IDNA and Punycode standards.

To retrieve the name of the local host, use the static QHostInfo::localHostName () 函數。

QHostInfo uses the mechanisms provided by the operating system to perform the lookup. As per {https://tools.ietf.org/html/rfc6724}{RFC 6724} there is no guarantee that all IP addresses registered for a domain or host will be returned.

注意: Since Qt 4.6.1 QHostInfo is using multiple threads for DNS lookup instead of one dedicated DNS thread. This improves performance, but also changes the order of signal emissions when using lookupHost () compared to previous versions of Qt.

注意: 從 Qt 4.6.3 起, QHostInfo is using a small internal 60 second DNS cache for performance improvements.

另請參閱 QAbstractSocket , RFC 3492 ,和 RFC 6724 .

成員類型文檔編製

enum QHostInfo:: HostInfoError

This enum describes the various errors that can occur when trying to resolve a host name.

常量 描述
QHostInfo::NoError 0 查找成功。
QHostInfo::HostNotFound 1 No IP addresses were found for the host.
QHostInfo::UnknownError 2 齣現未知錯誤。

另請參閱 error () 和 setError ().

成員函數文檔編製

QHostInfo:: QHostInfo ( int id = -1)

Constructs an empty host info object with lookup ID id .

另請參閱 lookupId ().

QHostInfo:: QHostInfo (const QHostInfo & other )

構造副本為 other .

QHostInfo:: ~QHostInfo ()

Destroys the host info object.

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

Aborts the host lookup with the ID id , as returned by lookupHost ().

另請參閱 lookupHost () 和 lookupId ().

QList < QHostAddress > QHostInfo:: addresses () const

Returns the list of IP addresses associated with hostName (). This list may be empty.

範例:

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

另請參閱 setAddresses (), hostName (),和 error ().

QHostInfo::HostInfoError QHostInfo:: error () const

Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError .

另請參閱 setError () 和 errorString ().

QString QHostInfo:: errorString () const

If the lookup failed, this function returns a human readable description of the error; otherwise "Unknown error" is returned.

另請參閱 setErrorString () 和 error ().

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

Looks up the IP address(es) for the given host name . The function blocks during the lookup which means that execution of the program is suspended until the results of the lookup are ready. Returns the result of the lookup in a QHostInfo 對象。

If you pass a literal IP address to name instead of a host name, QHostInfo will search for the domain name for the IP (i.e., QHostInfo will perform a reverse lookup). On success, the returned QHostInfo will contain both the resolved domain name and IP addresses for the host name.

另請參閱 lookupHost ().

QString QHostInfo:: hostName () const

Returns the name of the host whose IP addresses were looked up.

另請參閱 setHostName () 和 localHostName ().

[static] QString QHostInfo:: localDomainName ()

Returns the DNS domain of this machine.

注意: DNS domains are not related to domain names found in Windows networks.

另請參閱 hostName ().

[static] QString QHostInfo:: localHostName ()

Returns this machine's host name, if one is configured. Note that hostnames are not guaranteed to be globally unique, especially if they were configured automatically.

This function does not guarantee the returned host name is a Fully Qualified Domain Name (FQDN). For that, use fromName () to resolve the returned name to an FQDN.

此函數返迴如同 QSysInfo::machineHostName ().

另請參閱 hostName () 和 localDomainName ().

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

Looks up the IP address(es) associated with host name name , and returns an ID for the lookup. When the result of the lookup is ready, the slot or signal member in receiver is called with a QHostInfo 自變量。 QHostInfo object can then be inspected to get the results of the lookup.

The lookup is performed by a single function call, for example:

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

The implementation of the slot prints basic information about the addresses returned by the lookup, or reports an error if it failed:

void MyWidget::lookedUp(const QHostInfo &host)
{
    if (host.error() != QHostInfo::NoError) {
        qDebug() << "Lookup failed:" << host.errorString();
        return;
    }
    const auto addresses = host.addresses();
    for (const QHostAddress &address : addresses)
        qDebug() << "Found address:" << address.toString();
}
					

If you pass a literal IP address to name instead of a host name, QHostInfo will search for the domain name for the IP (i.e., QHostInfo will perform a reverse lookup). On success, the resulting QHostInfo will contain both the resolved domain name and IP addresses for the host name. Example:

QHostInfo::lookupHost("4.2.2.1",
                      this, SLOT(lookedUp(QHostInfo)));
					

注意: There is no guarantee on the order the signals will be emitted if you start multiple requests with lookupHost().

另請參閱 abortHostLookup (), addresses (), error (),和 fromName ().

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

這是重載函數。

Looks up the IP address(es) associated with host name name , and returns an ID for the lookup. When the result of the lookup is ready, the slot or signal function in receiver is called with a QHostInfo 自變量。 QHostInfo object can then be inspected to get the results of the lookup.

注意: There is no guarantee on the order the signals will be emitted if you start multiple requests with lookupHost ().

該函數在 Qt 5.9 引入。

另請參閱 abortHostLookup (), addresses (), error (),和 fromName ().

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

這是重載函數。

Looks up the IP address(es) associated with host name name , and returns an ID for the lookup. When the result of the lookup is ready, the functor is called with a QHostInfo 自變量。 QHostInfo object can then be inspected to get the results of the lookup.

The functor will be run in the thread that makes the call to lookupHost ; that thread must have a running Qt event loop.

注意: There is no guarantee on the order the signals will be emitted if you start multiple requests with lookupHost ().

該函數在 Qt 5.9 引入。

另請參閱 abortHostLookup (), addresses (), error (),和 fromName ().

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

這是重載函數。

Looks up the IP address(es) associated with host name name , and returns an ID for the lookup. When the result of the lookup is ready, the functor is called with a QHostInfo 自變量。 QHostInfo object can then be inspected to get the results of the lookup.

context is destroyed before the lookup completes, the functor will not be called. The functor will be run in the thread of context 。上下文綫程必須擁有正在運行的 Qt 事件循環。

注意: There is no guarantee on the order the signals will be emitted if you start multiple requests with lookupHost ().

該函數在 Qt 5.9 引入。

另請參閱 abortHostLookup (), addresses (), error (),和 fromName ().

int QHostInfo:: lookupId () const

Returns the ID of this lookup.

另請參閱 setLookupId (), abortHostLookup (),和 hostName ().

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

Sets the list of addresses in this QHostInfo to addresses .

另請參閱 addresses ().

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

Sets the error type of this QHostInfo to error .

另請參閱 error () 和 errorString ().

void QHostInfo:: setErrorString (const QString & str )

Sets the human readable description of the error that occurred to str if the lookup failed.

另請參閱 errorString () 和 setError ().

void QHostInfo:: setHostName (const QString & hostName )

Sets the host name of this QHostInfo to hostName .

另請參閱 hostName ().

void QHostInfo:: setLookupId ( int id )

Sets the ID of this lookup to id .

另請參閱 lookupId () 和 lookupHost ().

void QHostInfo:: swap ( QHostInfo & other )

Swaps host-info other with this host-info. This operation is very fast and never fails.

該函數在 Qt 5.10 引入。

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

賦值數據源於 other object to this host info object, and returns a reference to it.

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

移動賦值 other 到此 QHostInfo 實例。

注意: The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

該函數在 Qt 5.10 引入。