×
Namespaces

Variants
Actions

Getting Host's IP addresses and Interfaces

From Nokia Developer Wiki
Jump to: navigation, search

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
Symbian
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

Network.JPG

  • Showing Network Interface

Interface.JPG

The code example could be found at: File:Net.zip

This page was last modified on 11 October 2012, at 04:16.
174 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×