Qt提供的好几个类都可以获取到本机IP:IP4与IP6。这里,笔者使用QT获取本IP的方式如下:

头文件名: gethostIP_widget.h

头文件代码如下:

#ifndef GETHOSTIPWIDGET_H
#define GETHOSTIPWIDGET_H#include <QLabel>
#include <QString>
#include <QHostInfo>
#include <QHostAddress>class GetHostIP_Widget
{
public:GetHostIP_Widget();QString getLocalIp();
};#endif    //GETHOSTIPWIDGET_H

gethostIP_widget.cpp代码如下:

#include"gethostIP_widget.h"GetHostIP_Widget::GetHostIP_Widget()
{}/*函数名:getLocalIp函数参数:无函数返回值: 返回QString类型的 IP
*/
QString GetHostIP_Widget::getLocalIp()
{QString vAddress;
#ifdef _WIN32QHostInfo vHostInfo = QHostInfo::fromName(QHostInfo::localHostName());QList<QHostAddress> vAddressList = vHostInfo.addresses();
#elseQList<QHostAddress> vAddressList = QNetworkInterface::allAddresses();#endiffor(int i = 0; i < vAddressList.size(); i++){if (!vAddressList.at(i).isNull() &&vAddressList.at(i) != QHostAddress::LocalHost &&vAddressList.at(i).protocol() ==  QAbstractSocket::IPv4Protocol){vAddress = vAddressList.at(i).toString();//---检查显示的获取到的IP是否正确QLabel *b = new QLabel();b->setText("host IP : " + vAddress);b->show();break;//QT获取本机IP地址}}return vAddress;
}

这样,用类GetHostIP_Widget 实例化一个对象,调用成员函数getLocalIp就能得到本机的IP4的地址。

若要获取本机IP46的地址, 只需要将:

QAbstractSocket::IPv4Protocol

代码改成:

QAbstractSocket::IPv6Protocol

就能获取本机的IP6的地址了

附,调用Windows的库函数获取本机IP4的地址方法如下:

1.首先包含头文件: Winsock2.h

2.附上下面的代码即可:

hostent *pHostent = gethostbyname("");
QString temStr( inet_ntoa(*(IN_ADDR*)pHostent->h_addr_list[0]) );

这样,就能得到一个QString的IP4的地址了。

hostent的说明如下:

The hostent structure is used by functions to store information about a given host, such as host name, IPv4 address, and so forth. An application should never attempt to modify this structure or to free any of its components. Furthermore, only one copy of the hostent structure is allocated per thread, and an application should therefore copy any information that it needs before issuing any other Windows Sockets API calls.

typedef struct hostent {char FAR* h_name;char FAR  FAR** h_aliases;short h_addrtype;short h_length;char FAR  FAR** h_addr_list;
} HOSTENT, *PHOSTENT, FAR *LPHOSTENT;

Members

h_name

The official name of the host (PC). If using the DNS or similar resolution system, it is the Fully Qualified Domain Name (FQDN) that caused the server to return a reply. If using a local hosts file, it is the first entry after the IP address.

h_aliases

A NULL-terminated array of alternate names.

h_addrtype

The type of address being returned.

h_length

The length, in bytes, of each address.

h_addr_list

A NULL-terminated list of addresses for the host. Addresses are returned in network byte order. The macro h_addr is defined to be h_addr_list[0] for compatibility with older software.

下面是hostent 的官方demo的代码:

//----------------------
// Declare and initialize variables
struct hostent* remoteHost;
char* host_addr;
unsigned int addr;//----------------------
// User inputs IPv4 address of host
printf("Input IPv4 address of host: ");
host_addr = (char*) malloc(sizeof(char*)*16);
fgets(host_name, 16, stdin);// If the user input is an alpha name for the host, use gethostbyname()
// If not, get host by addr (assume IPv4)
if (!isalpha(host_addr[0]))    /* host address is an IPv4 address */
{if (strlen(host_addr) < 16){addr = inet_addr(host_addr);remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);}else{printf("error: Supplied host IPv4 address string is too large.\r\n");exit 255;}
}if (WSAGetLastError() != 0)
{if (WSAGetLastError() == 11001)printf("Host not found...\nExiting.\n");
}
elseprintf("error#:%ld\n", WSAGetLastError());// The remoteHost structure can now be used to
// access information about the host

Qt学习之路之获取本机IP相关推荐

  1. Qt学习之路_12(简易数据管理系统)

    原文地址为: Qt学习之路_12(简易数据管理系统) 前言 最近从大陆来到台湾,之间杂事很多,挤不出时间来更新博客- 这次主要是通过做一个简易的数据库管理系统,来学习在Qt中对数据库,xml,界面的各 ...

  2. QT学习之路2 学习笔记

    QT学习之路2 学习笔记 1.Qt 是一个著名的 C++ 应用程序框架.你并不能说它只是一个 GUI 库,因为 Qt 十分庞大,并不仅仅是 GUI 组件.使用 Qt,在一定程度上你获得的是一个&quo ...

  3. QT学习之路-资料收藏集锦

    目录 C++知识 Qt项目与文件 Qt基础类 Qt图形控件和GUI Qt图形绘制和图像处理 Qt与计算机硬件 常见问题 C++知识 C++ 枚举类型作用域的思考 C++中enum用法 C语言使用枚举类 ...

  4. 转载: Qt 学习之路 2归档

    Qt 学习之路 2归档 http://www.devbean.net/2012/08/qt-study-road-2-catelog/

  5. 对QT学习之路12-14的源代码补充与修正

    QT学习之路12-14的源代码有些不完整,为了更好的让大家学习,本人做了一点修正与补充,谢谢.源代码如下: 头文件: #ifndef MAINWINDOW_H #define MAINWINDOW_H ...

  6. java qt gui_工控编程,Qt 学习之路

    原标题:工控编程,Qt 学习之路 Qt 是一个著名的 C++ 库--或许并不能说这只是一个 GUI 库,因为 Qt 十分庞大,并不仅仅是 GUI.使用 Qt,在一定程序上你获得的是一个"一站 ...

  7. 《Qt 学习之路 2》

    Home / Qt 学习之路 2 / <Qt 学习之路 2>目录 <Qt 学习之路 2>目录 序 Qt 前言 Hello, world! 信号槽 自定义信号槽 Qt 模块简介 ...

  8. Qt网路与通信(获取本机IP、MAC、IPV6子网掩码等网络信息)

    Qt网路与通信(获取本机网络信息) 在网络应用中,经常需要获取本机的主机名/IP地址和硬件地址信息等网络信息.运用QHostInfo.QNetworkInterface.QNetworkAddress ...

  9. QT学习之路(一)ubuntu 18.04的Qt Creator在线安装

    文章目录 前言 一.准备工作 二.安装步骤 参考链接 前言 Qt是嵌入式开发的必备工具之一,在Linux下安装尤其重要. Qt是C++的一个库,或者说是开发框架,里面集成了一些库函数,提高开发效率. ...

最新文章

  1. [Unity WWW] 跨域访问解决方法
  2. 【博客话题】我的2011项目总结
  3. python的全局变量能暂存数据吗_Python 中的全局变量 局部变量
  4. 图的两种存储形式(邻接矩阵、邻接表)
  5. 持续集成mysql_持续集成环境搭建(5)zabbix搭建和使用
  6. QT pro文件解析
  7. MySQL数据库优化技巧(二)
  8. C++之map插入数据相同的key不能覆盖value解决办法
  9. 部署Spring Boot Angular App(Maven和Tomcat)的4种方法
  10. jdbc mysql user_tab_comments_MySQL学习(五)——使用JDBC完成用户表CRUD的操作
  11. jenkins运行日志时间与linux,查看日志
  12. html5做一个相册_HTML5最新版本介绍
  13. 第一章python绝对温标身体质量指数bmi
  14. java字符转为数字_java判断字符串是否可以转为数字
  15. Android系统搜索对话框(浮动搜索框)的使用
  16. office另存为pdf的加载项_你可能需要用到的office转换技巧
  17. SqlServer中将某字符串按照特定的字符分隔并返回临时表
  18. 【教程】Edraw Max使用教程:Edraw Max快速入门指南
  19. 《醉翁亭记》古文鉴赏
  20. Flink案例代码,面试题

热门文章

  1. linux卸载splunk,CentOS 7安装Splunk
  2. Linux运维工程师入门第一课-赵永刚-专题视频课程
  3. 2017年软上半年软考网络工程师级别考前冲刺之第四天-朱小平-专题视频课程
  4. 爬虫管理平台Crawlab v0.4.3发布(界面上点几下就可安装pip或npm依赖)
  5. 爬虫利器Pyppeteer的介绍和使用 爬取京东商城书籍信息
  6. session-cookie-前后端分离-跨域
  7. Elasticsearch实战(五)-倒排索引与分词
  8. python计算均线斜率_一根20日均线闯天下? ——量化回测“压箱底指标”
  9. 新增插件清理 可牛免费杀毒1.0 beta5版发布
  10. task_10时间序列