PDA

View Full Version : How to get the IP Address of the local machine in QT?



Gokulnathvc
16th July 2011, 10:27
How to get the IP Address of the local machine in Qt?

viulskiez
16th July 2011, 10:58
Look at QNetworkInterface documentation. This class has static methods allAddresses() to get available addresses, including localhost and broadcast address.

Gurusoft
8th September 2017, 16:51
I referred to this site (http://amin-ahmadi.com/2016/03/22/how-to-find-local-ip-addresses-in-qt/)

I was able to get the required local IP address:

foreach(const QNetworkInterface &qNetInterface, QNetworkInterface::allInterfaces()) {
foreach(const QNetworkAddressEntry &qNetInterfaceAddress, qNetInterface.addressEntries()) {
if (qNetInterfaceAddress.ip().protocol() == QAbstractSocket::IPv4Protocol
&& qNetInterfaceAddress.ip() != QHostAddress(QHostAddress::LocalHost)
&& qNetInterfaceAddress.netmask().toString() != "")
{
qDebug() << qNetInterfaceAddress.ip().toString();
qDebug() << qNetInterfaceAddress.netmask().toString();
}
}
}