我想要一种方法来获取与URL关联的IP的QStringList,我拥有那些拖曳方法,它们可以正常工作,但是我希望所有这些都在这样的单个方法上:QStringList DNSResolver(QString host);

 void Object::test()

{
    QHostInfo::lookupHost("www.google.com",this, SLOT(lookedUp(QHostInfo)));
}

void Object::lookedUp(const QHostInfo &host)
{

    if (host.error() != QHostInfo::NoError) {
        qDebug() << "Lookup failed:" << host.errorString();
    }

    foreach (const QHostAddress &address, host.addresses())
    {
         qDebug() << "Lookup success:" << address.toString();
    }
}

最佳答案

您可以使用QHostInfo::fromName ( const QString & name )在查询期间阻止:

QStringList DNSResolver(QString host)
{
    QHostInfo hostInfo;
    QHostInfo returnedHost = hostInfo.fromName(host);

    if (returnedHost.error() != QHostInfo::NoError) {
         qDebug() << "Lookup failed:" << returnedHost.errorString();
    }

    QStringList list;
    foreach (const QHostAddress &address, returnedHost.addresses())
    {
          list<<address.toString();
    }

    return list;

}

10-07 12:13