Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

  }

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

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

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;

I wanted to get eth1 IP address of my target machine. Answers provided above helped me to get what I wanted: This is how I wrote my function to get me the IP address of the network interface name eth1.

QNetworkInterface eth1Ip = QNetworkInterface::interfaceFromName("eth1");
QList<QNetworkAddressEntry> entries = eth1Ip.addressEntries();
if (!entries.isEmpty()) {
    QNetworkAddressEntry entry = entries.first();
    qDebug() << entry.ip();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top