Getting Host's IP addresses and Interfaces
This code example shows how to use the QNetworkInterface class to list the host's IP addresses and network interfaces. QNetworkInterface represents one network interface attached to the host where the program is being run. Each network interface may contain zero or more IP addresses, each of which is optionally associated with a netmask and/or a broadcast address.
Article Metadata
Code Example
Source file: Media:Net.zip
Tested with
Devices(s): Qt Creator IDE
Compatibility
Platform(s): S60 3rd Edition, S60 5th Edition
Article
Keywords: QNetworkInterface
Created: mind_freak
(27 Jun 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Functions
- This convenience function returns all IP addresses found on the host machine.
QNetworkInterface *inter=new QNetWorkInterface();
inter->allAddresses();
- Returns a listing of all the network interfaces found on the host machine.
QNetworkInterface *inter=new QNetWorkInterface();
inter->allInterfaces();
Source Code
Header File
#ifndef NET_H
#define NET_H
#include <QtGui/QWidget>
#include<QNetworkInterface>
#include<QList>
#include<QLabel>
#include<QHBoxLayout>
#include<QString>
#include<QHostAddress>
#include<QListWidget>
namespace Ui
{
class netClass;
}
class net : public QWidget
{
Q_OBJECT
public:
net(QWidget *parent = 0);
~net();
private:
QNetworkInterface *inter;
QLabel *lbl;
QHBoxLayout *lay;
QListWidget *item;
};
#endif // NET_H
Source File
#include "net.h"
#include "ui_net.h"
net::net(QWidget *parent)
: QWidget(parent)
{
QList<QHostAddress> list;
lbl=new QLabel(this);
lay=new QHBoxLayout(this);
item=new QListWidget(this);
inter=new QNetworkInterface();
list=inter->allAddresses();
QString str;
for (int i = 0; i < list.size(); ++i) {
str = list.at(i).toString();
item->addItem(str);
}
lay->addWidget(item);
setLayout(lay);
}
net::~net()
{
// No need to delete any object that got a parent that is properly deleted.
delete inter;
}
Source File for getting network Interface
#include "net.h"
#include "ui_net.h"
net::net(QWidget *parent)
: QWidget(parent)
{
QList<QNetworkInterface> list;
lbl=new QLabel(this);
lay=new QHBoxLayout(this);
item=new QListWidget(this);
inter=new QNetworkInterface();
list=inter->allInterfaces();
QString str;
for (int i = 0; i < list.size(); ++i) {
str = list.at(i).name();
item->addItem(str);
}
lay->addWidget(item);
setLayout(lay);
}
net::~net()
{
delete inter;
}
Screenshot
- Showing Host IP's
- Showing Network Interface
The code example could be found at: File:Net.zip


10 Sep
2009
At sometimes in web-based applications, we need to know the listing of host's IP address and network interfaces. This article simply represents the code for the same. The aricle contains introduction of the article and also presents about basic platform and APIs it has used. The class here used for doing this task is "QNetworkInterface". You should note that all the methods in the class are reentrant. "allAddress()" method of this class is used to obtain the IP address and "allInterface()" method is used to get the network interface.
The article has represented the code in a systematic manner. The article can be useful to beginner as well as intermediate developers.
18 Sep
2009
Article shows the use of the QNetworkInterface and QHostAddress. Header and source file code shows complete mode of application. Merging this to another application make that application very grand.