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.





Leave a Reply

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