Important: Please read the Qt Code of Conduct -

[SOLVED] hostinfo.h: how to get the right ip address?

  • below is the code that i use to get the ip address. the problem is that the ip address that i get is from the router. when i go online and check my ip address, it is different that what i get from the router. how to get the right ip address that the internet detects?

    @ info = QHostInfo::fromName( QHostInfo::localHostName() );
    ip = info.addresses();

  • Are you talking about the NAT of the router?

  • When you go online and check your IP, what you get is the IP of your modem. You most probably have a private network, with IP's like 192.178.0.x or something like that. Your modem is working as a gateway between the network of your ISP and your private network, using NAT.

    So, your code is returning the correct IP address. It is the IP address of your own machine, not that of your modem/router on the public network.

  • the problem is that the 192.X.X.X ip does not resolve to domain. is there a way to get an ip for NAT?

    below is the code that i use to try to resolve my ip to domain. is there another way to do this?

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"
    #include "QHostInfo"

    QHostInfo info;
    QList<QHostAddress> ip;

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)



    delete ui;

    void MainWindow::ipLookup(){

    info = QHostInfo::fromName( QHostInfo::localHostName()  );
    ip = info.addresses();
    // print ip address
    QHostInfo::lookupHost(ip[0].toString(), this, SLOT(domainLookup(QHostInfo)));


    void MainWindow::domainLookup(const QHostInfo &host)
    if (host.error() != QHostInfo::NoError) {
    ui->textEdit->append("Lookup failed:" + host.errorString());

    foreach (const QHostAddress &address, host.addresses()){
        if (ip[0].toString() == address.toString())
            ui->textEdit->append("no reverse DNS for this IP");
        else {
            // print domain


  • There is no real way to get the external address, that's kind of the point of NAT. This isn't a problem with your code, or with Qt, it's just how address translation works. You would either need to use a router specific way of finding the external address (eg. accessing it's web gui, or querying it via SNMP), or use an external service.

  • You could always use QNetworkAccessManager to grab data from That page just gives you your external IP address and nothing more..

    You could do something like so.
    @void MainWindow::getIP()
    QNetworkReply reply = accessManager->get(QNetworkRequest(""));
    connect(accessManager, SIGNAL(finished(QNetworkReply
    )), SLOT(assignIP(QNetworkReply*)));

    void MainWindow::assignIP(QNetworkReply *reply)
    if(reply->error() == QNetworkReply::NoError)
    ipAddress = reply->readAll();
    QMessageBox::information(this, "Network error", "Could not get your external IP address. Your interwebs seem to be down.");

  • thank you ShadowMoses for the code. do you also have code for resolve to domain because my code does not resolve to domain for some reason?

  • After you obtain the external IP through the method above you can just use QHostInfo like so.
    @QHostInfo info = QHostInfo::fromName(ipAddress);
    QString nameAddress = info.hostName();@

    Just remember that is the blocking way of doing things. If you want you can do it with slots as well.

    Something like this should work.
    @QHostInfo::lookupHost(ipAddress, this, SLOT(assignHostName(QHostInfo)));

    MainWindow::assignHostName(QHostInfo info)
    //some error checking
    hostAddress = info.hostName();

  • thank you ShadowMoses. you have been lots of help :)

Log in to reply