Important: Please read the Qt Code of Conduct -

Finding local and public IP address with Qt?

  • Hello,

    how exactly am I supposed to get my local IP address with Qt? my public IP address? The following gets a list of IP addresses, but I don't know what to do with it afterward:

    auto list = QHostInfo::fromName(QHostInfo::localHostName()).addresses();

    Thanks in advance.

  • Lifetime Qt Champion

    It returns a list.
    QList<QHostAddress> list = QHostInfo::fromName(QHostInfo::localHostName()).addresses();
    for (int var = 0; var < list.size(); ++var) {
    qDebug() << list[var].XXXXXX();
    XXXXX being what you are interested in;

  • @mrjj Okay but what is list[0].toString()? what is list[1].toString()? Are these strings of my local or public IP address? What's the size of that list?

  • Lifetime Qt Champion

    Its a list of QHostAddress objects.

    this size of list is how many QHostAddress there is in list.

  • I did understand that, but I was asking my question in this particular instance:

    auto list = QHostInfo::fromName(QHostInfo::localHostName()).addresses();

    Since the local host is my computer, why should it (or any other computer for that matter) have more than 1 IP address? Why should there be a list at all? Could you explain to me how I am supposed to get my local IP address with this list? And how I'm supposed to get my public IP address? I'm a bit confused.

  • Lifetime Qt Champion

    A pc can have many interfaces. Like one for lan , one for wifi.
    You could also have more than one netcard.
    So that's why it's a list.
    Not sure how you would get local iP from a QHostAddress.

    People seems to use other calls:

    toString() seems to return the iP
    i get
    "" <<<<<<<<<<<<<< this is my real one.

    For your WAN (public IP)
    please see here
    QNetworkReply reply = accessManager->get(QNetworkRequest(""));

  • Okay thanks for your help. I'm just so surprised/disappointed that it didn't seem to occur to Qt's coding team that we might want to know our own IP addresses... Every single solution looks like a Rube Goldberg machine.

  • Lifetime Qt Champion

    well there is not easy / straight way to get IP
    as its not how it works.

    Most PC have multiple interfaces (and hence ips)
    and none can tell which is the correct one in just a single call.

    For external IP, these is no way to know except to look it up like
    linked showed.

    Network is inherently complex and getting IP is small part of using the network classes among the
    1000 other things it also must do.

  • @mrjj It doesn't seem that complex for the people running the SFML: see their page.

    Both the local and public addresses are only a static function away with the SFML. I wasn't expecting things to get over-complicated without this library. I'm probably going to use it for network purposes after all.

  • Lifetime Qt Champion

    Have you tried it ?
    It most likely just return the first found.
    Anyway, Qt is much, much bigger than SFML so you cannot expect
    all things to be as neatly wrapped up as SFML.

    But why don't you just use SFML if it suits better?

  • @mrjj I do think I'm about to use the SFML. I initially wanted to use Qt as much as possible so that I don't have to link anything non-Qt to my project. I agree, though, with your other point: I've noticed how practical and extensive Qt can get, as opposed to the SFML. It's just sad to see that the SFML is sometimes much easier to use for very straightforward issues.

Log in to reply