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
QNetworkInterface returns lots of addresses. you must filter them, to get desirable result:
foreach (const QNetworkInterface &netInterface, QNetworkInterface::allInterfaces()) {
QNetworkInterface::InterfaceFlags flags = netInterface.flags();
if( (bool)(flags & QNetworkInterface::IsRunning) && !(bool)(flags & QNetworkInterface::IsLoopBack)){
foreach (const QNetworkAddressEntry &address, netInterface.addressEntries()) {
if(address.ip().protocol() == QAbstractSocket::IPv4Protocol)
qDebug() << address.ip().toString();
}
}
}