QHostInfo class provides an static function (lookupHost) that you can use to get any host’s (including a website’s) IP address quickly and efficiently.
Here is an example of how you can get Google’s IP Address using Qt/C++:
First of all call lookupHost function and pass a callback function (SLOT) to it:
QHostInfo::lookupHost("google.com", this, SLOT(lookedUp(QHostInfo)));
This Slot is defined in your class (i.e. MainWindow) like this:
public slots:
void lookedUp(const QHostInfo &host);
Finally lookedUp function’s implementation would look something like this:
void MainWindow::lookedUp(const QHostInfo& host)
{
if (host.error() != QHostInfo::NoError)
{
qDebug() << host.errorString();
}
else
{
for (int i = 0; i < host.addresses().count(); i++)
qDebug() << host.addresses().at(i).toString();
}
}
You can replace the lines starting with qDebug() to show your own custom messages and use the values for any other purpose.
Note: By running this code I got the following IP address which is google.com’s IP address. You can copy and paste it to your browser’s address bar and check it out for yourself: