参考网址:http://blog.csdn.net/wjs1033/article/details/22697063

1、环境 Win7x64、Qt5.5.1(x86)、vs2013_ultimate_up5(x86)

  1.1、?.h / ?.cpp 都保存成“UTF-8 + BOM”格式(这样,源码里面中文/特殊符号,使用中文注释,就不会有 error或warnning了)

  1.2、?.h / ?.cpp 都保存成“UTF-8 + BOM”格式 的话,qDebug()输出中文的时候 全是乱码...

    ?.h / ?.cpp 都保存成“UTF-8”           格式 的话,qDebug()输出中文的时候 小部分是乱码...

    ZC: 暂时 先将 ?.h / ?.cpp 都保存成“UTF-8”格式,以后再研究 这些字符串编码的 中文乱码的事情吧...

2、代码:

  2.1、?.pro

#-------------------------------------------------
#
# Project created by QtCreator 2016-11-14T16:10:00
#
#------------------------------------------------- QT += core gui \
network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = NetworkInterfaceZ
TEMPLATE = app SOURCES += main.cpp\
MainWindow.cpp HEADERS += MainWindow.h FORMS += MainWindow.ui

  2.2、MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QHostInfo> // http://blog.csdn.net/wjs1033/article/details/22697063 namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow(); private slots:
void on_pbtnMsg01_clicked(); void on_pbtnMsg02_clicked(); void on_pbtnMsg03_clicked(); void on_pbtnMsg04_clicked(); void on_pbtnMsg05_clicked(); private:
Ui::MainWindow *ui; public slots:
void LookupHostZ(const QHostInfo &_hostInfo);
}; #endif // MAINWINDOW_H

  2.3、MainWindow.cpp

#include "MainWindow.h"
#include "ui_MainWindow.h" #include <QNetworkInterface> MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
} MainWindow::~MainWindow()
{
delete ui;
} // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // 枚举本机的网络连接并获取其属性
void MainWindow::on_pbtnMsg01_clicked()
{
int i=, j=;
QList<QNetworkInterface> networkInterface = QNetworkInterface::allInterfaces();
for (QList<QNetworkInterface>::const_iterator it = networkInterface.constBegin(); it != networkInterface.constEnd(); ++it)
{
qDebug() << "[" << i << "] : " << (*it).name();
qDebug() << " " << (*it).humanReadableName();
qDebug() << " " << (*it).hardwareAddress(); //获取连接地址列表
QList<QNetworkAddressEntry> addressEntriesList = (*it).addressEntries();
for (QList<QNetworkAddressEntry>::const_iterator jIt = addressEntriesList.constBegin(); jIt != addressEntriesList.constEnd(); ++jIt)
{
qDebug() << "\t(" << j << ") :";
//输出 ip
qDebug() << "\t\tIP : " <<(*jIt).ip().toString();
//输出 netmask
qDebug() << "\t\tnetmask(子网掩码) : " << (*jIt).netmask().toString();
qDebug() << "\t\tBroadcast(广播地址) : "<< (*jIt).broadcast().toString();
qDebug() << "";
j ++;
}
i ++;
}
} // 只枚举ip地址的简洁方式
void MainWindow::on_pbtnMsg02_clicked()
{
QList<QHostAddress> list = QNetworkInterface::allAddresses();
{
foreach(QHostAddress address,list)
{
if(address.protocol() == QAbstractSocket::IPv4Protocol)
qDebug() << address.toString();
}
}
} // 获取本机主机名 及 IPv4地址
void MainWindow::on_pbtnMsg03_clicked()
{
QString strLocalHostName = QHostInfo::localHostName(); // 获取主机名
qDebug() << "本地主机名 : " << strLocalHostName; QHostInfo info = QHostInfo::fromName(strLocalHostName);// 根据上边获得的主机名来获取本机的信息
// QHostInfo的address函数获取本机ip地址
// QHostAddress类是管理ip地址的类,所有的ip都归这个类管理。
foreach (QHostAddress address, info.addresses())
{
if(address.protocol() == QAbstractSocket::IPv4Protocol)//只取ipv4协议的地址
qDebug() << "IP(IPv4Protocol) : " << address.toString();
}
} // 通过 主机名 获取IP地址
void MainWindow::on_pbtnMsg04_clicked()
{
// 以主机名获取ip (会调用slot函数LookupHostZ)
QHostInfo::lookupHost("www.baidu.com", this, SLOT(LookupHostZ(QHostInfo)));
} // 通过IP地址 反向查询 主机名
void MainWindow::on_pbtnMsg05_clicked()
{
// 通过ip地址获取主机名 (会调用slot函数LookupHostZ)
QHostInfo::lookupHost("192.168.1.201", this, SLOT(LookupHostZ(QHostInfo)));
} // 响应QHostInfo::lookupHost(...) 的slot函数
void MainWindow::LookupHostZ(const QHostInfo &_hostInfo)
{
// 输出IPv4的IP地址
foreach (QHostAddress address, _hostInfo.addresses())
{
if(address.protocol() == QAbstractSocket::IPv4Protocol)//只取ipv4协议的地址
qDebug() << "LookupHostZ - IP(IPv4Protocol) : " << address.toString();
} // 输出主机名
qDebug() << _hostInfo.hostName();
}

  2.4、MainWindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>518</width>
<height>354</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="pbtnMsg01">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>211</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>枚举本机的网络连接并获取其属性</string>
</property>
</widget>
<widget class="QPushButton" name="pbtnMsg02">
<property name="geometry">
<rect>
<x>20</x>
<y>40</y>
<width>211</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>只枚举ip地址的简洁方式</string>
</property>
</widget>
<widget class="QPushButton" name="pbtnMsg03">
<property name="geometry">
<rect>
<x>20</x>
<y>90</y>
<width>211</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>获取本机主机名及IPv4地址</string>
</property>
</widget>
<widget class="QPushButton" name="pbtnMsg04">
<property name="geometry">
<rect>
<x>20</x>
<y>140</y>
<width>211</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>通过主机名获取IP地址</string>
</property>
</widget>
<widget class="QPushButton" name="pbtnMsg05">
<property name="geometry">
<rect>
<x>20</x>
<y>170</y>
<width>211</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>通过IP地址反向查询主机名</string>
</property>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

  2.5、界面截图

3、程序运行输出:

  3.1、枚举本机的网络连接并获取其属性

[ 0 ] :  "{7558E85A-8386-4A5F-A5BF-4A4F22792AA4}"
"无线网络连接"
"34:23:87:61:DF:31"
( 0 ) :
IP : "fe80::a4d3:7dfd:62cf:558d%14"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : "" ( 1 ) :
IP : "169.254.85.141"
netmask(子网掩码) : ""
Broadcast(广播地址) : "" [ 1 ] : "{3B25081D-362E-42E8-8FDF-B6D5A99824CF}"
"本地连接"
"28:D2:44:40:5B:C1"
( 2 ) :
IP : "fe80::e58e:6f0e:df37:e8ed%12"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : "" ( 3 ) :
IP : "192.168.1.233"
netmask(子网掩码) : "255.255.255.0"
Broadcast(广播地址) : "192.168.1.255" [ 2 ] : "{B5D6033F-8002-4428-80A3-1974993AF12A}"
"Bluetooth 网络连接"
"34:23:87:61:DF:32"
( 4 ) :
IP : "fe80::e0b7:f552:784e:9fe%11"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : "" ( 5 ) :
IP : "169.254.9.254"
netmask(子网掩码) : ""
Broadcast(广播地址) : "" [ 3 ] : "{8376C30F-73E1-4029-B465-AB5449D3812E}"
"VMware Network Adapter VMnet1"
"00:50:56:C0:00:01"
( 6 ) :
IP : "fe80::29c7:6a:97d7:bb0f%19"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : "" ( 7 ) :
IP : "192.168.131.1"
netmask(子网掩码) : "255.255.255.0"
Broadcast(广播地址) : "192.168.131.255" [ 4 ] : "{1D0B26ED-F9CB-43A4-AD64-202A6D727BFA}"
"VMware Network Adapter VMnet8"
"00:50:56:C0:00:08"
( 8 ) :
IP : "fe80::a567:53d5:a0b5:cd63%20"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : "" ( 9 ) :
IP : "192.168.181.1"
netmask(子网掩码) : "255.255.255.0"
Broadcast(广播地址) : "192.168.181.255" [ 5 ] : "{846EE342-7039-11DE-9D20-806E6F6E6963}"
"Loopback Pseudo-Interface 1"
""
( 10 ) :
IP : "::1"
netmask(子网掩码) : "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
Broadcast(广播地址) : "" ( 11 ) :
IP : "127.0.0.1"
netmask(子网掩码) : ""
Broadcast(广播地址) : "" [ 6 ] : "{DA5C1007-9FC1-4637-AFBF-CFC713356DDA}"
"isatap.{3B25081D-362E-42E8-8FDF-B6D5A99824CF}"
"00:00:00:00:00:00:00:E0"
( 12 ) :
IP : "fe80::5efe:c0a8:1e9%15"
netmask(子网掩码) : "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
Broadcast(广播地址) : "" [ 7 ] : "{B6A0553B-7196-4747-87B8-D1307AA54F7E}"
"Teredo Tunneling Pseudo-Interface"
"00:00:00:00:00:00:00:E0"
( 13 ) :
IP : "fe80::100:7f:fffe%13"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : "" [ 8 ] : "{B61A7FDB-E0C1-42CC-A324-54F35789A245}"
"isatap.{8376C30F-73E1-4029-B465-AB5449D3812E}"
"00:00:00:00:00:00:00:E0"
( 14 ) :
IP : "fe80::5efe:c0a8:8301%16"
netmask(子网掩码) : "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
Broadcast(广播地址) : "" [ 9 ] : "{6B68472C-AA56-44A8-A9AE-FFEF1A4ED978}"
"isatap.{7558E85A-8386-4A5F-A5BF-4A4F22792AA4}"
"00:00:00:00:00:00:00:E0"
[ 10 ] : "{BAAD8784-EFFA-4C76-AE9E-8074A9965DBA}"
"isatap.{B5D6033F-8002-4428-80A3-1974993AF12A}"
"00:00:00:00:00:00:00:E0"
[ 11 ] : "{7F3F01D3-A615-4238-8453-632DCB4C13C6}"
"isatap.{1D0B26ED-F9CB-43A4-AD64-202A6D727BFA}"
"00:00:00:00:00:00:00:E0"
( 15 ) :
IP : "fe80::5efe:c0a8:b501%21"
netmask(子网掩码) : "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
Broadcast(广播地址) : ""

  3.2、只枚举ip地址的简洁方式

"169.254.85.141"
"192.168.1.233"
"169.254.9.254"
"192.168.131.1"
"192.168.181.1"
"127.0.0.1"

  3.3、获取本机主机名 及 IPv4地址 (这里的 控制台输出 中文会有乱码)

"本地主机???: " "33-PC"
IP(IPv4Protocol) : "192.168.1.233"
IP(IPv4Protocol) : "192.168.131.1"
IP(IPv4Protocol) : "192.168.181.1"

  3.4、通过 主机名 获取IP地址

LookupHostZ - IP(IPv4Protocol) :  "180.97.33.108"
LookupHostZ - IP(IPv4Protocol) : "180.97.33.107"
"www.baidu.com"

  3.5、通过IP地址 反向查询 主机名 (这个执行的时候,用时较长)

LookupHostZ - IP(IPv4Protocol) :  "192.168.1.201"
"ZHEJIANG001"

  3.6、

4、获取本机的 第一个IPv4地址的字符串 (Windows下)

  传入参数:_strNetworkInterfaceHumanReadableName : 网络接口卡的名称(类似"无线网络连接"、"本地连接"等)

QString Widget::getIP(QString _strNetworkInterfaceHumanReadableName)
{
//QString str01 = QString::fromLocal8Bit("本地连接"); QList<QNetworkInterface> networkInterface = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface intf, networkInterface)
{
bool b = false;
if (_strNetworkInterfaceHumanReadableName == NULL)
b = true;
else
{
QString str = intf.humanReadableName();
if (str.startsWith(_strNetworkInterfaceHumanReadableName, Qt::CaseInsensitive))
b = true;
} if (b)
{
QList<QNetworkAddressEntry> listNAE = intf.addressEntries();
foreach (QNetworkAddressEntry nae, listNAE)
{
QHostAddress ha = nae.ip();
if (ha.protocol() == QAbstractSocket::IPv4Protocol)
return ha.toString();
}
}
}
return NULL;
}

5、

Qt5获取网卡/IP等信息的更多相关文章

  1. 获取本地IP地址信息

    2012-06-05    /// <summary>         /// 获取本地IP地址信息         /// </summary>         void G ...

  2. Jsp调用淘宝IP地址库获取来访IP详细信息

    Jsp调用淘宝IP地址库获取来访IP详细信息   示例网页点击:www.trembler.cn/ipinfo/ipinfo(服务器有其他用处,页面已失效) String ip = request.ge ...

  3. Qt5获取本机网络信息

    获取本机网络信息 在pro文件中加入如下代码 QT += network widget.h中的代码如下 #ifndef WIDGET_H #define WIDGET_H #include <Q ...

  4. LINUX下QT与C语言通过网卡名获取网卡IP与MAC

    1.QT下 QString RuntimeConfig::ipAddress(QString network) { QList<QNetworkAddressEntry> list; QS ...

  5. C语言实现ifconfig获取网卡接收和发送流量统计

    在Windows下我们可以利用ipconfig命令获取网卡的相关信息,在Linux下命令是ifconfig 我们可以获取的信息更为丰富,其中包括网卡接收和发送的流量,用C语言实现这个命令并不是一件简单 ...

  6. GetAdaptersInfo获取网卡配置和Ip地址信息

    一台机器上可能不只有一个网卡,但每一个网卡只有一个MAC地址,而每一个网卡可能配置有多个IP地址:如平常的笔记本电脑中,就会有无线网卡和有线网卡(网线接口)两种:因此,如果要获得本机所有网卡的IP和M ...

  7. Qt之获取本机网络信息(MAC, IP等等,很全)

    经常使用命令行来查看一些计算机的配置信息. 1.首先按住键盘上的“开始键+R键”,然后在弹出的对话框中输入“CMD”,回车 另外,还可以依次点击 开始>所有程序>附件>命令提示符 2 ...

  8. C++通过GetAdapatersInfo获取网卡配置信息

    DWORD GetAdaptersInfo( PIP_ADAPTER_INFO pAdapterInfo, //指向一个缓冲区,用来取得IP_ADAPTER_INFO结构列表 PULONG pOutB ...

  9. Lodop获取客户端主网卡ip地址是0.0.0.0

    LODOP技术手册的GET_SYSTEM_INFO篇,LODOP可以用语句获取到客户端很多信息,NetworkAdapter.1.IPAddress是主网卡IP地址,通常情况下是没问题的,不过如果当前 ...

随机推荐

  1. MUI事件管理

    模块:事件管理 http://dev.dcloud.net.cn/mui/event/ 事件绑定: 除了可以使用addEventListener()方法监听某个特定元素上的事件外, 也可以使用.on( ...

  2. 有关velocity的资料(等待整理)

    proxy-target-class="true" 与proxy-target-class="false"的区别: proxy-target-class属性值决 ...

  3. shell 输出文件内容

    cat $filepath | while read line; do echo $line ; done #!/bin/bash #filepath=/opt/jenkins_home/worksp ...

  4. 徐州网络赛C-Cacti Lottery【DFS】

    54.19% 2000ms 262144K Morgana is playing a game called cacti lottery. In this game, morgana has a 3 ...

  5. 多线程下载图片,滑动tableView崩溃--资源抢夺问题

    最近练习使用NSoperation模拟SDWebImage下载图片,发生了崩溃的问题,还专门写博客记录这件事情: http://www.cnblogs.com/tufei7/p/7074030.htm ...

  6. QQ 空间过滤器 for V8

    最近 QQ空间升级到 V8 版本,做了很大的调整, 我也做了升级,由于时间关系,功能暂时只有 模块过滤,其他过滤请等待后续更新,谢谢大家的支持! 刚刚上线,不知道你们能否看到 https://chro ...

  7. android 控件加圆角

    1.新建一个radius_border.xml <shape xmlns:android="http://schemas.android.com/apk/res/android&quo ...

  8. centos shell基础 alias 变量单引号 双引号 history 错误重定向 2>&1 jobs 环境变量 .bash_history source配置文件 nohup & 后台运行 cut,sort,wc ,uniq ,tee ,tr ,split, paste cat> 2.txt <<EOF 通配符 glob模式 发邮件命令mail 2015-4-8 第十二节课

    centos shell基础知识 alias  变量单引号 双引号   history 错误重定向 2>&1  jobs  环境变量 .bash_history  source配置文件 ...

  9. Spring—切点表达式

    摘要: Spring中的AspectJ切点表达式函数 切点表达式函数就像我们的GPS导航软件.通过切点表达式函数,再配合通配符和逻辑运算符的灵活运用,我们能很好定位到我们需要织入增强的连接点上.经过上 ...

  10. [LeetCode]83. Remove Duplicates from Sorted List(排序链表去重)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...