说明

  • 需要添加 network模块
  • 本文介绍的函数将读取所有本机IP,包括 ipv4和ipv6
  • 本文演示版本 qt5.14

头文件

#include <QHostAddress>
#include <QNetworkInterface>

读取代码

QStringList get_ip_()
{
QStringList list_ip;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
int nListSize = ipAddressesList.size(); for (int i = 0; i < nListSize; ++i)
list_ip << ipAddressesList.at(i).toString(); return list_ip;
}

调用

	qint32 index = 0;

	QStringList list_ip = get_ip_();
for (auto item : list_ip)
{
qDebug() << "index=" << ++ index << ", ip=" << item;
}

结果

说明

  • 上面的图片中可见,ipv6的地址中含有网卡编号, 我们只需要%左边的ip地址即可, 于是修正上面的代码即可得到
QStringList get_ip_()
{
QStringList list_ip;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
int nListSize = ipAddressesList.size(); for (int i = 0; i < nListSize; ++i)
{
QString str_tmp = ipAddressesList.at(i).toString();
int split_pos = str_tmp.indexOf('%'); if (-1 != split_pos)
{
if ( 0 != split_pos)
split_pos = split_pos - 1;
str_tmp = str_tmp.left(split_pos);
} list_ip << str_tmp;
} return list_ip;
}

结果

qt5读取所有本机IP的更多相关文章

  1. QT5下获取本机IP地址、计算机名、网络连接名、MAC地址、子网掩码、广播地址

    获取主机名称 /* * 名称:get_localmachine_name * 功能:获取本机机器名称 * 参数:no * 返回:QString */ QString CafesClient::get_ ...

  2. 读取本地外网IP地址

    读取本地外网IP地址. 根据启动并运行的网卡名称,找到本机实际的IP地址(已知当前运行的无线网卡名包含某一个字符) import java.net.InterfaceAddress; import j ...

  3. C# 获取网页数据、获取本机IP 分类: C# 2014-12-16 14:59 308人阅读 评论(0) 收藏

    说明: (1) http://www.3322.org/dyndns/getip 这个网址可以获取本机IP,读取的内容就是本机IP (2)方法经测试,可以实现. (3)参考:http://www.cn ...

  4. 如何获取本机IP

    GetLocalHost 直接通过InetAddress.getLocalHost()来获取,其主要逻辑如下 InetAddress.getLocalHost(): String hostname = ...

  5. Linux 获取本机IP、MAC地址用法大全

    getifaddrs()和struct ifaddrs的使用,获取本机IP ifaddrs结构体定义如下: struct ifaddrs { struct ifaddrs *ifa_next; /* ...

  6. java获取本机ip(排除虚拟机等一些ip)最终解,总算找到方法了

    本文参考https://blog.csdn.net/u011809209/article/details/77236602 本文参考https://blog.csdn.net/yinshuomail/ ...

  7. 获取本机IP地址

    这里有两种方法: //获取本机IP - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; ) { ...

  8. .net中c#获取本机IP地址实例代码

    * 在使用前,一定要注意在头部加上引用: using System.Net; 代码如下: using System; using System.Collections.Generic; using S ...

  9. 本机ip+端口不能访问web server,外部却可以访问

    本机ip+端口不能访问web server,外部却可以访问! 这个奇葩的问题困扰了我好久,别人通过ip访问我的server一切正常,自己却访问不了,一度怀疑win10的问题,久寻无果! 最后关闭ads ...

随机推荐

  1. Yet Another Minimization Problem

    Yet Another Minimization Problem 一个很显然的决策单调性. 方程是很显然的 $ f_i = \min{f_{j-1} + w(j,i)} $ . 它具有决策单调性,可以 ...

  2. 水平梯度在sigma坐标对应形式

    sigma 坐标变换 一般 \(\sigma\) 坐标转换方程为 \[\sigma = \frac{z-\eta}{D} = \frac{z-\eta}{H+\eta} \] 转换后水深 z 范围由原 ...

  3. 解决Gitlab的The remote end hung up unexpectedly错误,解决RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large问题

    解决Gitlab的The remote end hung up unexpectedly错误 解决RPC failed; HTTP 413 curl 22 The requested URL retu ...

  4. 【R shiny】一些应用记录

    目录 DT和downloadButton应用 downloadButton 中验证结果输出 添加进度条 如何确保仅在使用Shiny按下操作按钮时才触发操作 其他 DT和downloadButton应用 ...

  5. AnnotationHub, clusterProfiler 进行GO,KEGG注释

    ️ AnnotationHub 目前最新的工具包叫做AnnotationHub,顾名思义,就是注释信息的中装站.通过它,能找到了几乎所有的注释资源.如果没有,你还可以根据已有的数据用它提供的函数进行构 ...

  6. Redis—怎么查看Linux有没有安装Redis,如何启动Redis

    1.检测是否有安装redis-cli和redis-server [root@localhost bin]# whereis redis-cli redis-cli: /usr/bin/redis-cl ...

  7. 《手把手教你》系列技巧篇(四十七)-java+ selenium自动化测试-判断元素是否显示(详解教程)

    1.简介 webdriver有三种判断元素状态的方法,分别是isEnabled,isSelected 和 isDisplayed,其中isSelected在前面的内容中已经简单的介绍了,isSelec ...

  8. C++ 德才论

    输入样例: 14 60 80 10000001 64 90 10000002 90 60 10000011 85 80 10000003 85 80 10000004 80 85 10000005 8 ...

  9. linux shell中的条件判断语句

    http://bbs.chinaunix.net/thread-396805-1-1.html shell 判断语句 流程控制 "if" 表达式 如果条件为真则执行then后面的部 ...

  10. 转 onSaveInstanceState()和onRestoreInstanceState()使用详解

    转 https://www.jianshu.com/p/27181e2e32d2 背景 如果系统由于系统约束(而不是正常的应用程序行为)而破坏了Activity,那么尽管实际 Activity实例已经 ...