在.pro文件中添加

QT += network

在networkinformation.h中添加

#ifndef NEWWORKINFORMATION_H
#define NEWWORKINFORMATION_H

#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QMessageBox>
#include <QHostInfo>
#include <QNetworkInterface>
class newworkinformation : public QWidget
{
    Q_OBJECT

public:
    newworkinformation(QWidget *parent = nullptr);
    ~newworkinformation();
    void getHostInformation();
public slots:
    void slotDetail();
private:
    QLabel *hostLabel;
    QLineEdit *LineEditLocalHostName;
    QLabel *ipLabel;
    QLineEdit *LineEditAddress;
    QPushButton *detailBtn;
    QGridLayout *mainLayout;

};
#endif // NEWWORKINFORMATION_H

在networkinformation.cpp中添加

#include "newworkinformation.h"

newworkinformation::newworkinformation(QWidget *parent)
    : QWidget(parent)
{
    hostLabel =  new QLabel(tr("主机名:"));
    LineEditLocalHostName = new QLineEdit;
    ipLabel =new QLabel(tr("地址:"));
    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 newworkinformation::getHostInformation()
{
    QString localHostName = QHostInfo::localHostName();
    //获取本机主机名
    LineEditLocalHostName->setText(localHostName);
    QHostInfo hostInfo = QHostInfo::fromName(localHostName);
    //通过主机名获取信息
    QList<QHostAddress> listAddress =hostInfo.addresses();
    //获取主机的Ip地址列表
    if(!listAddress.isEmpty())
    {
        LineEditAddress->setText(listAddress.at(0).toString());
    }//在不为空的情况下,使用第一个地址

}
void newworkinformation::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);
}

newworkinformation::~newworkinformation()
{
}


相关函数请在前面博客中QT资料大全
中查找!!!

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐