How to Find Local IP Addresses in Qt

Posted on Posted in Qt Tutorials, Tutorials

Below is the approach I use to get all of the IP addresses for the local computer. Note that this method also gives you all Subnet Masks which you can then use to find all IP addresses in a subnet, say for scanning a Home Network or any similar purpose.

QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
for(int i=0; i<interfaces.count(); i++)
{
QList<QNetworkAddressEntry> entries = interfaces.at(i).addressEntries();
for(int j=0; j<entries.count(); j++)
{
if(entries.at(j).ip().protocol() == QAbstractSocket::IPv4Protocol)
{
qDebug() << entries.at(j).ip().toString();
qDebug() << entries.at(j).netmask().toString();
}
}
}

You can replace the lines starting with qDebug() with whatever you want to do with the IP addresses and subnet masks.





One thought on “How to Find Local IP Addresses in Qt

  1. I am newbee for Qt. The above logic will give the details of local IP interfaces. If we want the all interfaces of specific IP then what to do. e.g. my machine if is 10.130.23.12 and I want see the all interfaces of IP 127.0.0.1. Then how to do this using QNetworkInterface

Leave a Reply

Your email address will not be published. Required fields are marked *