Getting Host's IP addresses and Interfaces
Jump to navigation
Jump to search
Functions[edit | edit source]
- 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[edit | edit source]
Header File[edit | edit source]
- 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[edit | edit source]
- 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[edit | edit source]
- 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[edit | edit source]
- Showing Host IP's
- Showing Network Interface
The code example could be found at: File:Net.zip