Qt-网络与通信-获取本机网络信息

举报
DreamLife 发表于 2022/04/14 22:48:27 2022/04/14
1.3k+ 0 0
【摘要】 在网络应用中,经常需要获取本机主机名和IP地址和硬件地址等信息。运用QHostInfo、QNetworkInterface、QNetworkAddressEntry可以获得本机的网络信息。   上运行截图     这里需要注意的,在Qt5.80 VS的版本中,有的字符“:”中文版本的,会导致编...

在网络应用中,经常需要获取本机主机名和IP地址和硬件地址等信息。运用QHostInfo、QNetworkInterface、QNetworkAddressEntry可以获得本机的网络信息。

上运行截图

这里需要注意的,在Qt5.80 VS的版本中,有的字符“:”中文版本的,会导致编译错误。

第一步,需要再pro文件中加入 QT+= network

.h文件


      #ifndef WIDGET_H
      #define WIDGET_H
      #include <QWidget>
      #include <QLabel>
      #include <QPushButton>
      #include <QLineEdit>
      #include <QGridLayout>
      #include <QMessageBox>
      #include <QHostInfo>
      #include <QNetworkInterface>
      class Widget : public QWidget
      {
          Q_OBJECT
      public:
         Widget(QWidget *parent = 0);
          ~Widget();
         void getHostInformation();
      public slots:
         void slotDetail();
      private:
          QLabel *hostLabel;
          QLineEdit *LineEditLocalHostName;
          QLabel *ipLabel;
          QLineEdit *LineEditAddress;
          QPushButton *detailBtn;
          QGridLayout *mainLayout;
      };
      #endif // WIDGET_H
  
 

.cpp文件


      #include "widget.h"
      #include <QDebug>
      Widget::Widget(QWidget *parent)
          : QWidget(parent)
      {
          hostLabel = new QLabel(tr("主机名:"));
          LineEditLocalHostName = new QLineEdit;
          ipLabel = new QLabel(tr("IP 地址:"));
          LineEditAddress = new QLineEdit;
          detailBtn = new QPushButton(tr("详细"));
          mainLayout = new QGridLayout(this);
          mainLayout->addWidget(hostLabel,0,0);
          mainLayout->addWidget(LineEditLocalHostName,0,1);
          mainLayout->addWidget(ipLabel,1,0);
          mainLayout->addWidget(LineEditAddress,1,1);
          mainLayout->addWidget(detailBtn,2,0,1,2);
         getHostInformation();
         connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotDetail()));
      }
      Widget::~Widget()
      {
      }
      void Widget::getHostInformation()
      {
          QString localHostName = QHostInfo::localHostName();
          LineEditLocalHostName->setText(localHostName);
          QHostInfo hostInfo = QHostInfo::fromName(localHostName);
          QList<QHostAddress> listAddress = hostInfo.addresses();
         qDebug()<<listAddress;
         if(!listAddress.isEmpty())
          {
              LineEditAddress->setText(listAddress.at(4).toString());
          }
      }
      void Widget::slotDetail()
      {
          QString detail="";
          QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();
         //(a)
         for(int i=0;i<list.count();i++)
          {
              QNetworkInterface interface=list.at(i);
              detail=detail+tr("设备:")+interface.name()+"\n";
             //(b)
              detail=detail+tr("硬件地址:")+interface.hardwareAddress()+"\n";
             //(c)
              QList<QNetworkAddressEntry> entryList=interface.addressEntries();
             //(d)
             for(int j=1;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);
      }
  
 

工程地址:https://gitee.com/DreamLife-Technology_DreamLife/NetworkInformation

文章来源: dreamlife.blog.csdn.net,作者:DreamLife.,版权归原作者所有,如需转载,请联系作者。

原文链接:dreamlife.blog.csdn.net/article/details/79405220

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。