码迷,mamicode.com
首页 > 其他好文 > 详细

【Qt5开发及实例】28、获取本机网络信息

时间:2015-02-28 08:58:22      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:qt5   tcp   ip   网络   c++   

networkinformation.h

/**
* 书本:【Qt5开发及实例】
* 功能:获取本机网络信息
* 文件:networkinformation.h
* 时间:2015年2月5日14:51:33
* 作者:cutter_point
*/
#ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QHostInfo>
#include <QNetworkInterface>
#include <QMessageBox>

class NetworkInformation : public QWidget
{
  Q_OBJECT

public:
  NetworkInformation(QWidget *parent = 0);
  ~NetworkInformation();

  void getHostInformation();    //得到主机信息

public slots:
  void slotDetail();    //展示详细信息

private:
  //界面布局
  QLabel *hostLabel;
  QLineEdit *LineEditLocalHostName;
  QLabel *ipLabel;
  QLineEdit *LineEditAddress;

  QPushButton *detailBtn;

  QGridLayout *mainLayout;

};

#endif // NETWORKINFORMATION_H

networkinformation.cpp

/**
* 书本:【Qt5开发及实例】
* 功能:获取本机网络信息
* 文件:networkinformation.cpp
* 时间:2015年2月5日14:51:33
* 作者:cutter_point
*/
#include "networkinformation.h"

NetworkInformation::NetworkInformation(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()));
}

//  void slotDetail();    //展示详细信息
void NetworkInformation::slotDetail()
{
  QString detail = "";
  QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();   //得到所有的接口

  for(int i = 0; i < list.count(); ++i)
    {
      QNetworkInterface interface = list.at(i);   //得到第i个接口
//      detail = detail + tr("设备:") + interface.name() + "\n";
      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);
    }
}


//void getHostInformation();    //得到主机信息
void NetworkInformation::getHostInformation()
{
  QString localHostName = QHostInfo::localHostName();   //得到主机名
  LineEditLocalHostName->setText(localHostName);

  QHostInfo hostinfo = QHostInfo::fromName(localHostName);
  QList<QHostAddress> listAddress = hostinfo.addresses();   //主机所有地址

  if(!listAddress.isEmpty())
    {
//      listAddress.at(2)
//      LineEditAddress->setText(listAddress.first().toString());   //取第一个
//      LineEditAddress->setText(static_cast<QString>(listAddress.length()));   //取第一个
      LineEditAddress->setText(listAddress.at(3).toString());
    }

}


NetworkInformation::~NetworkInformation()
{

}

结果显示

技术分享







【Qt5开发及实例】28、获取本机网络信息

标签:qt5   tcp   ip   网络   c++   

原文地址:http://blog.csdn.net/cutter_point/article/details/43973145

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!