/** * 书本:【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
/** * 书本:【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() { }
原文地址:http://blog.csdn.net/cutter_point/article/details/43973145