#include <QNetworkInterface>
#include <QList> void MainWindow::getIPPath()
{ QString strIpAddress; QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); // 获取第一个本主机的IPv4地址 int nListSize = ipAddressesList.size(); for (int i = 0; i < nListSize; ++i) { if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address())
{ strIpAddress = ipAddressesList.at(i).toString(); break; } } // 如果没有找到,则以本地IP地址为IP if (strIpAddress.isEmpty())
{
qDebug() << strIpAddress;
strIpAddress = QHostAddress(QHostAddress::LocalHost).toString();
}
qDebug() << "IP:" << strIpAddress;
} void MainWindow::getMacPath()
{
QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表 int nCnt = nets.count(); QString strMacAddr = ""; for(int i = 0; i < nCnt; i ++) { // 如果此网络接口被激活并且正在运行并且不是回环地址,则就是我们需要找的Mac地址 if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning) && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack)) { strMacAddr = nets[i].hardwareAddress(); break; } }
qDebug() << "Mac:" << strMacAddr;
}

  

QT获取本机IP和Mac地址的更多相关文章

  1. 获取本机IP、mac地址、计算机名

    python获取本机IP.mac地址.计算机名 在python中获取ip地址和在php中有很大不同,我们先来看一下python 获得本机MAC地址: >>> import uuid ...

  2. python获取本机IP、mac地址、计算机名

    在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: 1 2 3 4 import uuid de ...

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

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

  4. JAVA获取本机IP和Mac地址

       在项目中,时常需要获取本机的Ip或是Mac地址,进行身份和权限验证,本文就是通过java代码获取ip和Mac. package com.svse.query;import java.net.In ...

  5. ioctrl 获取本机IP及MAC地址

    通过使用ioctl可以获得本机的一些信息,这里记录获得interface IP及MAC的过程. 1:ioctl 函数的作用是什么 man ioctl: DESCRIPTION The ioctl() ...

  6. java 获取本机ip及mac地址

    package com.achun.test; import java.net.Inet4Address;import java.net.Inet6Address;import java.net.In ...

  7. c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存

    我们在利用C#开发桌面程序(Winform)程序的时候, 经常需要获取一些跟系统相关的信息, 以下这些代码获取能有些用处. c#中如何获取本机用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统 ...

  8. Qt获取本机IP地址

    Qt获取本机IP地址: Qt版本:4.8.6 #include <QtNetwork/QNetworkInterface.h> QString ipAddr; QList<QHost ...

  9. qt获取本机ip

    //获取本机IP QString getIP(QString localHost) { QString ipAddr; #if 0 QList<QHostAddress> AddressL ...

随机推荐

  1. [Swift]LeetCode263. 丑数 | Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  2. [Swift]LeetCode348. 设计井字棋游戏 $ Design Tic-Tac-Toe

    Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...

  3. [Swift]LeetCode554. 砖墙 | Brick Wall

    There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The b ...

  4. [Swift]LeetCode965. 单值二叉树 | Univalued Binary Tree

    A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...

  5. Python Matplotlib.pyplot plt 中文显示

    话不多说,上代码 # -*- coding: UTF-8 -*- import matplotlib.pyplot as plt from matplotlib.font_manager import ...

  6. Django+Bootstrap+Mysql 搭建个人博客(一)

    1.1.环境搭建 (1)虚拟环境 mkvirtualenv website pip install django==1.11.7 (2)创建项目和app:website和blog (3)设置中文set ...

  7. Python常见面试题

    Q 1:Python 有哪些特点和优点? 作为一门编程入门语言,Python 主要有以下特点和优点: ● 可解释● 具有动态特性● 面向对象● 简明简单● 开源● 具有强大的社区支持当然,实际上 Py ...

  8. AspNetCore 使用log4net+IExceptionFilter 记录错误日志

    错误日志的好处我就不说了,大家都心里有数,那今天浩子就给大家说一说基本的错误日志吧这次通过log4net记录日志. 原来写过一个关于Nlog的日志框架,传送门为:https://www.cnblogs ...

  9. Chapter 4 Invitations——15

    He slouched off, back toward the school. 他无精打采的回去了学校. I heard a low chuckle. 我听到了低声的笑. Edward was wa ...

  10. IDEA搭建Spring Boot项目

    所需工具 新建项目 创建一个login控制器 写入两个注释 import导入项会自动添加@RestController@RequestMapping(value = "/login" ...