打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【2011.03.17】如何使用Qt获得主机IP地址及接口

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

获得网络接口的代码 #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
   显示网络接口         
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
IP地址基础知识
Qt获取计算机MAC地址
Android:手机扫描局域网所有ip,并进行socket通讯
追踪入侵者方法
使用HCL模拟器配置DHCP相关项目
华为路由器单臂路由实例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服