QHostAddress address; // Set the IP address to 127.0.0.1 for IPv4 if (address.setAddress("127.0.0.1")) { qDebug() << "IPv4: " << address.toString(); } // Set the IP address to ::1 for IPv6 if (address.setAddress("::1")) { qDebug() << "IPv6: " << address.toString(); }
QHostInfo info = QHostInfo::fromName("www.google.com"); QListThis code uses the QHostInfo class to look up the IP addresses associated with the hostname "www.google.com". The code then displays all of the IP addresses found, distinguishing between IPv4 and IPv6 addresses. The QHostAddress class is included in the Qt Network package library.addresses = info.addresses(); foreach (QHostAddress address, addresses) { if (address.protocol() == QAbstractSocket::IPv4Protocol) { qDebug() << "IPv4: " << address.toString(); } else if (address.protocol() == QAbstractSocket::IPv6Protocol) { qDebug() << "IPv6: " << address.toString(); } }