Qt / C ++ - 将QHostAddress转换为QString

时间:2013-06-04 02:16:57

标签: c++ qt

如何以IPv4格式将QHostAddress转换为QString

1 个答案:

答案 0 :(得分:6)

使用QHostAddress::toString()

<强>更新

如果您有一个封装IPv6地址的QHostAddress实例(即QHostAddress::protocol()返回QAbstractSocket::IPv6Protocol),则必须先将地址转换为IPv4地址,然后再转换IPv4地址为QString

QHostAddress ip6Address;
bool conversionOK = false;
QHostAddress ip4Address(ip6Address.toIPv4Address(&conversionOK));
QString ip4String;
if (conversionOK)
{
    ip4String = ip4Address.toString();
}

值得注意的是,对于Qt 4,上述IP6到IP4的转换将不起作用。它仅适用于Qt 5.