Qt获取计算机名、用户名、IP地址、MAC

这个硬件信息的获取需要使用到 network 模块

# 1. 在pro文件中添加 QT += network
# 需要用到的头文件
#include <QHostInfo> // 计算机名
#include <QStandardPaths> // 用户名(获取家目录,删除其它字段只保留用户名)
#include <QNetworkInterface> // 网卡信息
cout << "当前计算机名:" << QHostInfo::localHostName();
cout << "当前登陆用户:" << QStandardPaths::writableLocation(QStandardPaths::HomeLocation).section("/", -1, -1);
QList<QHostAddress> AddressList = QNetworkInterface::allAddresses();
foreach(QHostAddress address, AddressList){
if(address.protocol() == QAbstractSocket::IPv4Protocol &&
address != QHostAddress::Null &&
address != QHostAddress::LocalHost){
if (address.toString().contains("127.0.")){
continue;
}
cout << "当前IP地址:" << address.toString();
break;
}
}
// ---> 依次输出系统中每张网卡的详细信息
QString detail="";
QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();
for(int i=0;i<list.count();i++)
{
QNetworkInterface interface=list.at(i);
detail=detail+tr("设备:")+interface.name()+"\n";
detail=detail+tr("硬件地址:")+interface.hardwareAddress()+"\n";
QList<QNetworkAddressEntry> entryList=interface.addressEntries();
for(int j=0;j<entryList.count();j++)
{
QNetworkAddressEntry entry=entryList.at(j);
detail=detail+"\t"+tr("IP 地址:")+entry.ip().toString()+"\n";
detail=detail+"\t"+tr("子网掩码:")+entry.netmask().toString()+"\n";
detail=detail+"\t"+tr("广播地址:")+entry.broadcast().toString()+"\n";
}
}
QMessageBox::information(this,tr("Detail"),detail);
posted @   看不见的R  阅读(759)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示