Get local IP address in Qt

51,886

Solution 1

Use QNetworkInterface::allAddresses()

const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
for (const QHostAddress &address: QNetworkInterface::allAddresses()) {
    if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost)
         qDebug() << address.toString();
}

Solution 2

QNetworkInterface::allAddresses() will give you the network addresses. You can then filter the results to IPv4 addresses that are not loopback addresses:

QList<QHostAddress> list = QNetworkInterface::allAddresses();

 for(int nIter=0; nIter<list.count(); nIter++)

  {
      if(!list[nIter].isLoopback())
          if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol )
        qDebug() << list[nIter].toString();

  }

Solution 3

If you require more information than just IP addresses (like the subnet), you have to iterate over all the interfaces.

QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
QNetworkInterface eth;

foreach(eth, allInterfaces) {
    QList<QNetworkAddressEntry> allEntries = eth.addressEntries();
    QNetworkAddressEntry entry;
    foreach (entry, allEntries) {
        qDebug() << entry.ip().toString() << "/" << entry.netmask().toString();
    }
}

Solution 4

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();
        }
    }
}

Solution 5

Here is the code I implemented to get: name, IP, netmask and mac address of localhost.

   QString localhostname =  QHostInfo::localHostName();
   QString localhostIP;
   QList<QHostAddress> hostList = QHostInfo::fromName(localhostname).addresses();
   foreach (const QHostAddress& address, hostList) {
       if (address.protocol() == QAbstractSocket::IPv4Protocol && address.isLoopback() == false) {
            localhostIP = address.toString();
       }
   }
   QString localMacAddress;
   QString localNetmask;
   foreach (const QNetworkInterface& networkInterface, QNetworkInterface::allInterfaces()) {
       foreach (const QNetworkAddressEntry& entry, networkInterface.addressEntries()) {
           if (entry.ip().toString() == localhostIP) {
               localMacAddress = networkInterface.hardwareAddress();
               localNetmask = entry.netmask().toString();
               break;
           }
       }
   }
   qDebug() << "Localhost name: " << localhostname;
   qDebug() << "IP = " << localhostIP;
   qDebug() << "MAC = " << localMacAddress;
   qDebug() << "Netmask = " << localNetmask;
Share:
51,886
sashoalm
Author by

sashoalm

Updated on April 15, 2020

Comments

  • sashoalm
    sashoalm about 4 years

    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 server for a Symbian phone and I want to show the IP address the FTP client should connect to.

  • Valentin H
    Valentin H about 11 years
    +1: unfortunately, it returns also addresses of virtual machines, virtual boxes, etc. I've used a quick and dirty fix by checking if last number isn't 1 (usually IP of a getaway) && address.toString().section( ".",-1,-1 ) != "1". I'm still looking for possibility to ckeck if found local Ip also has a standard getaway.