Get local IP address in Qt

sashoalm picture sashoalm · Dec 12, 2012 · Viewed 45.6k times · Source

Is there a cross-platform way to get the local IP address (i.e. something that looks like 192.168.1.49) of the computer using Qt?

I want to make an FTP server for a Symbian phone and I want to show the IP address the FTP client should connect to.

Answer

benjarobin picture benjarobin · Dec 12, 2012

Use QNetworkInterface::allAddresses()

const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
for (const QHostAddress &address: QNetworkInterface::allAddresses()) {
    if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost)
         qDebug() << address.toString();
}