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. SQL Server 2005实现负载均衡的详细介绍
  2. Maven项目整合讲义(Eclipse版)
  3. POJ 1279 Art Gallery 半平面交 多边形的核
  4. R学习笔记:运行时间记录
  5. 软件测试(四)之 PrintPrimes
  6. TensorFlow报错FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated......(亲测)
  7. Qt中查看ui_xxx.h文件方法
  8. Android 开发笔记 ProgressDialog的Back健关闭
  9. Silverlight.XNA(C#)跨平台3D游戏研发手记:(八)向Windows Phone移植之2D跨平台迁移
  10. Linus送出圣诞礼物:发布Linux 4.20,超35万行代码
  11. IS-IS详解(十二)——IS-IS 路由过载、管理标记和主机名映射
  12. 规则绝对公平时,社会财富的流向谁?
  13. 账户系统,余额与体现
  14. 计算机控制实验比例环节,自动控制原理实验一典型环节的电路模拟与软件仿真...
  15. 【Python计量】RESET模型设定偏误检验
  16. 【微软内推】微软2023届校园招聘开始啦
  17. 天天向上的力量---python持续的力量
  18. Dango Web 开发指南 学习笔记 1
  19. 所谓的牛逼,都是用苦逼换来的
  20. OpenCV-颜色通道的分离、合并

热门文章

  1. 安装Mysql时出现服务未启动(start service)解决办法
  2. iOS Handle Refunds 处理退款 --- WWDC20
  3. 【附源码和详细的公式推导】Minimum Snap轨迹生成,闭式求解Minimum Snap问题,机器人轨迹优化,多项式轨迹路径生成与优化
  4. 实习程序员帮上级“背锅”成习惯,转正后还来?实习生没人权啊!
  5. B - Pell数列
  6. 如何理解三极管的三种放大电路
  7. MySQL触发器的题_mysql触发器问题
  8. 谁是卧底python代码_[代码全屏查看]-机器人陪你玩“谁是卧底游戏
  9. python-谁是卧底小游戏
  10. 【六爻】明天新同事会入职吗?