如何使用 Qt 获得主机IP地址及接口 详细介绍

移动开发
本文介绍的是如何使用 Qt 获得主机IP地址及接口,关于QT网络请关注本文末尾,有更多资料可以供你参考。

如何使用 Qt 获得主机IP地址接口是本文介绍的内容,不多说,先来看内容。QNetworkInterface类提供了一个主机IP地址和网络接口的列表。

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

前提条件---下载安装***版的Qt for Symbian - Installation packages

传统函数返回host机器上所有的ip地址

  1. QNetworkInterface *inter=new QNetWorkInterface();  
  2. inter->allAddresses(); 

返回host机器上发现的所有网络接口的列表

  1. QNetworkInterface *inter=new QNetWorkInterface();  
  2. inter->allInterfaces(); 

头文件 #ifndef NET_H

  1. #define NET_H  
  2. #include <QtGui/QWidget> 
  3. #include<QNetworkInterface> 
  4. #include<QList> 
  5. #include<QLabel> 
  6. #include<QHBoxLayout> 
  7. #include<QString> 
  8. #include<QHostAddress> 
  9. #include<QListWidget> 
  10. namespace Ui  
  11. {  
  12. class netClass;  
  13. }  
  14. class net : public QWidget  
  15. {  
  16. Q_OBJECT  
  17. public:  
  18. net(QWidget *parent = 0);  
  19. ~net();  
  20. private:  
  21. QNetworkInterface *inter;  
  22. QLabel *lbl;  
  23. QHBoxLayout *lay;  
  24. QListWidget *item;  
  25. };  
  26. #endif // NET_H 

源文件 #include "net.h"

  1. #include "ui_net.h"  
  2.    
  3. net::net(QWidget *parent)  
  4. : QWidget(parent)  
  5. {  
  6. QList<QHostAddress> list;  
  7. lbl=new QLabel(this);  
  8. lay=new QHBoxLayout(this);  
  9. item=new QListWidget(this);  
  10. inter=new QNetworkInterface();  
  11. list=inter->allAddresses();  
  12. QString str;  
  13. for (int i = 0; i < list.size(); ++i) {  
  14.    
  15. str = list.at(i).toString();  
  16. item->addItem(str);  
  17. }  
  18. lay->addWidget(item);  
  19. setLayout(lay);  
  20. }  
  21. net::~net()  
  22. {  
  23. // No need to delete any object that got a parent that is properly deleted.  
  24. delete inter;  

获得网络接口的代码 #include "net.h"

  1. #include "ui_net.h"  
  2. net::net(QWidget *parent)  
  3. : QWidget(parent)  
  4. {  
  5. QList<QNetworkInterface> list;  
  6. lbl=new QLabel(this);  
  7. lay=new QHBoxLayout(this);  
  8. item=new QListWidget(this);  
  9. inter=new QNetworkInterface();  
  10. list=inter->allInterfaces();  
  11. QString str;  
  12. for (int i = 0; i < list.size(); ++i) {  
  13. str = list.at(i).name();  
  14. item->addItem(str);  
  15. }  
  16. lay->addWidget(item);  
  17. setLayout(lay);  
  18. }  
  19. net::~net()  
  20. {  
  21. delete inter;  

显示host IP  

如何使用 Qt 获得主机IP地址及接口 详细介绍

显示网络接口     

如何使用 Qt 获得主机IP地址及接口 详细介绍

小结:如何使用 Qt 获得主机IP地址接口 详细介绍到这里就介绍完了,希望本文对你有所帮助!关于网络的更多内容,请参考编辑推荐。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-06-27 09:36:58

Ubuntu Qt Creator

2011-06-29 15:02:25

Qt 模板库

2011-06-22 17:09:50

QT 进程 通信

2011-08-29 10:22:48

QtWebkit 模块HTML文档

2010-06-02 09:01:20

Linux core

2016-12-08 12:47:05

Linux在线主机IP地址

2011-06-29 14:56:28

Qt Creator 快捷捷

2022-08-18 23:20:03

数据泄露IP 地址隐私

2011-07-22 16:37:01

java接口

2011-07-11 16:55:31

Java

2023-02-01 09:15:41

2015-07-10 09:08:52

IP地址IP地址冲突

2011-06-22 13:27:04

QT QMap

2009-09-18 19:21:17

C#接口

2012-12-10 09:24:04

2010-09-27 13:51:23

接口IP地址故障

2012-11-27 12:02:20

路由器IPARP

2011-06-23 09:00:04

QT QODBC 数据库

2010-06-02 13:53:26

IPv6地址

2023-11-16 09:01:37

Hadoop数据库
点赞
收藏

51CTO技术栈公众号