CDT
CDT

Reputation: 10651

Qt/C++ - Convert QHostAddress to QString

How can I convert QHostAddress to QString in IPv4 format ?

Upvotes: 3

Views: 12644

Answers (1)

RA.
RA.

Reputation: 7777

Use QHostAddress::toString().

Update

If you have a QHostAddress instance that encapsulates an IPv6 address (that is, QHostAddress::protocol() returns QAbstractSocket::IPv6Protocol), you must first convert the address to the IPv4 address and then convert the IPv4 address to a QString:

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

It's also worth noting that the above IP6 to IP4 conversion will not work for Qt 4. It will only work for Qt 5.

Upvotes: 10

Related Questions