Nominate our 2022 Qt Champions!

Getting the Ip address the from active NIC on the local computer

  • Hello all, I'm trying to figure out a way to get just the ip address from the active NIC on a local computer.

    I figured out how to narrow it down to just the ipv4 addresses but I'm also getting with it, the loopback, the actual address im looking for, some random one that im not sure where its from, and the default dhcp address.

    so far my code looks like this

    @void MainWindow::on_pushButton_clicked()

    QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
    QHostAddress address;
    foreach(address, addresses)
        if(address.protocol() == QAbstractSocket::IPv4Protocol)

    } @

    and my output is this


    any suggestion?

  • Hi,

    well, you should query your network interfaces in a different way.

    QListIterator< QNetworkInterface > interface(QNetworkInterface::allInterfaces());

    while (interface.hasNext())
    QNetworkInterface intf(;

    if ((interface.flags() & QNetworkInterface::IsUp) &&
    (interface.flags() & QNetworkInterface::IsRunning) &&
    (interface.flags() & ~QNetworkInterface::IsLoopBack))
    QListIterator< QNetworkAddressEntry > networkAddressEntry(QNetworkInterface::addressEntries());

    while (networkAddressEntry.hasNext())
      QNetworkAddressEntry netEntry(;
      if (netEntry.protocol() == QAbstractSocket::IPv4Protocol)


    Maybe there're errors in this code due to the fact that I've typed it right now, without a compiler, but I hope that the concept is clear.


  • I hope this will help...

    Its similar to Antonio Di Monaco sample.

  • hi rahul, can u convert it to a link

  • Thanks guys for the help!!

    So basically i should query the interfaces instead of just the ip addresses and use the interface flags and checking the protocol to knock out the info i don't need.

    taking from the examples i did this and got what i was looking for

    @foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
    if(interface.flags().testFlag(QNetworkInterface::IsUp) &&
    foreach(QNetworkAddressEntry entry, interface.addressEntries())
    if(entry.ip().protocol() == QAbstractSocket::IPv4Protocol)

    Thank you guys so much! Ya the Best!

  • Hi all,
    thanks for the help, this code helped me a lot.

    Does anybody know how to determine, if local IP comes from DHCP or is a static bind?
    Does anybody know how to assign new binding method (dhcp/static)

    and if static
    Does anybody know how to assign ip, mask and standard gateway.

    portable code is great, but I can be happy with Fedora code....

    Thanks in advance.

Log in to reply