Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

获得主机IP地址及接口

From Wiki
Jump to: navigation, search
Article Metadata

代码示例
源文件: Media:Net.zip

兼容于
平台:
Symbian

文章
hoolee 在 11 Jan 2010 创建
最后由 hamishwillee 在 11 Oct 2012 编辑

Contents

简介

QNetworkInterface类提供了一个主机IP地址和网络接口的列表。

QNetworkInterface提供了一个依附于主机的网络接口,程序可以在上面运行。每个网络接口包括0~n个IP地址,每个都可选的配有网络掩码或广播地址。

前提条件


功能

  • 传统函数返回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;
 
}

获得网络接口的代码

#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;
}

截图

  • 显示host IP

Network.JPG

  • 显示网络接口

Interface.JPG

示例代码: File:Net.zip

This page was last modified on 11 October 2012, at 01:19.
63 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.

×