QNetworkInterface类提供了一个主机IP地址和网络接口的列表。
QNetworkInterface提供了一个依附于主机的网络接口,程序可以在上面运行。每个网络接口包括0~n个IP地址,每个都可选的配有网络掩码或广播地址。
前提条件---下载安装最新版的Qt for Symbian - Installation packages
传统函数返回host机器上所有的ip地址
QNetworkInterface *inter=new QNetWorkInterface();
inter->allAddresses();
返回host机器上发现的所有网络接口的列表
QNetworkInterface *inter=new QNetWorkInterface();
inter->allInterfaces();
头文件 #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
源文件 #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;
}
联系客服