这个硬件信息的获取需要使用到 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); |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构