当前位置: 首页 > news >正文

Qt——实现一个获取本机网络信息的界面

效果展现

在这里插入图片描述
在这里插入图片描述

代码实现

networkinformation.h

#ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>class NetworkInformation : public QMainWindow
{Q_OBJECTpublic:NetworkInformation(QWidget *parent = nullptr);~NetworkInformation();void getHostInfo();public slots:void slotDetail();private:QLabel * hostNameLabel;QLineEdit * hostNameLineEdit;QLabel * ipLabel;QLineEdit * ipLineEdit;QPushButton * detailBtn;};
#endif // NETWORKINFORMATION_H

networkinformation.cpp

#include "networkinformation.h"
#include <QGridLayout>
#include <QHostInfo>
#include <QNetworkInterface>
#include <QMessageBox>
#include <QDebug>NetworkInformation::NetworkInformation(QWidget *parent): QMainWindow(parent){QWidget * widget = new QWidget();this->setCentralWidget(widget);hostNameLabel = new QLabel(tr("主机名:"));hostNameLineEdit = new QLineEdit;ipLabel = new QLabel(tr("IP地址:"));ipLineEdit = new QLineEdit;detailBtn = new QPushButton(tr("详细"));QGridLayout * mainLayout = new QGridLayout;mainLayout->addWidget(hostNameLabel,0,0);mainLayout->addWidget(hostNameLineEdit,0,1);mainLayout->addWidget(ipLabel,1,0);mainLayout->addWidget(ipLineEdit,1,1);mainLayout->addWidget(detailBtn,2,0,1,2);widget->setLayout(mainLayout);//获取主机信息getHostInfo();//点击详细按钮 显示详细的 网络信息connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotDetail()));}void NetworkInformation::getHostInfo(){//主机名QString hostName = QHostInfo::localHostName();hostNameLineEdit->setText(hostName);//ip地址QHostInfo hostInfo = QHostInfo::fromName(hostName);QList<QHostAddress> list = hostInfo.addresses();qDebug()<<"size = "<<list.count()<<endl;for(int i = 0;i < list.count();i++){qDebug()<<list.at(i).toString()<<endl;}if(list.count() >= 3){ipLineEdit->setText(list.at(3).toString());}}void NetworkInformation::slotDetail(){QString detail = "";QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();for(int i = 0;i < list.count();i++){QNetworkInterface interface = list.at(i);detail += tr("设备:") + interface.name() + "\n";detail += tr("硬件地址:") + interface.hardwareAddress() + "\n";QList<QNetworkAddressEntry> entryList = interface.addressEntries();for(int j = 0;j < entryList.count();j++){QNetworkAddressEntry entry = entryList.at(j);detail += "\t" + tr("IP地址:") + entry.ip().toString() + "\n";detail += "\t" + tr("子网掩码:") + entry.netmask().toString() + "\n";detail += "\t" + tr("广播地址:") + entry.broadcast().toString() + "\n";}}qDebug()<<detail<<endl;QMessageBox::information(this,tr("Detail"),detail);}NetworkInformation::~NetworkInformation()
{
}

main.cpp

#include "networkinformation.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);NetworkInformation w;w.show();return a.exec();
}
http://www.lryc.cn/news/57931.html

相关文章:

  • 全面深入了解接口自动化,看完还不会我报地址
  • Python 小型项目大全 61~65
  • Hlog
  • 学编程应该选择什么操作系统?
  • Oracle基础部分二(伪列/表、单个函数、空值处理、行列转换、分析函数、集合运算)
  • c/c++:原码,反码,补码和常见的数据类型取值范围,溢出
  • Java题目训练——年终奖和迷宫问题
  • ORACLE EBS系统应用基础概述(1)
  • 电子科技大学信息与通信工程学院2023考研复试总结
  • 神经网络激活函数
  • 2.C 语言基本语法
  • Qt 6.5 LTS 正式发布
  • Linux权限提升—定时任务、环境变量、权限配置不当、数据库等提权
  • Python爬虫——使用requests和beautifulsoup4库来爬取指定网页的信息
  • 基于Java3D的网络三维技术的设计与实现
  • python机器学习数据建模与分析——数据预测与预测建模
  • Flink系列-6、Flink DataSet的Transformation
  • Java-类的知识进阶
  • C# | 上位机开发新手指南(六)摘要算法
  • 测试工程师:“ 这锅我不背 ” ,面对灵魂三问,如何回怼?
  • 【Java闭关修炼】SpringBoot-SpringMVC概述和入门
  • pdf转换器免费版哪种好用:Aiseesoft PDF Converter Ultimate | 无损转word转Excel转PPT转图片啥都行!!!
  • 革新市场营销,突破瓶颈:关键词采集和市场调查的秘密武器
  • 3年测试经验只会“点点点”,不会自动化即将面临公司淘汰?沉淀100天继续做测试
  • python:异常处理与文件操作(知识点详解+代码展示)
  • SpringBoot 过滤器和拦截器(三十八)
  • Memcache论文总结——Lec16
  • 父子组件传值问题
  • Redis大key问题
  • 00后卷王的自述,我难道真的很卷?