How to get the IP Address of the local machine in Qt?
How to get the IP Address of the local machine in Qt?
Look at QNetworkInterface documentation. This class has static methods allAddresses() to get available addresses, including localhost and broadcast address.
~ We are nothing in this universe ~
I referred to this site
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();
}
}
}
Bookmarks