#include <QHostInfo>
#include <QNetworkInterface>

//获取本机主机名
QString localHostName = QHostInfo::localHostName();

//根据主机名获取相关主机信息,包括IP地址等
QHostInfo hostInfo = QHostInfo::fromName(localHostName);

//获取主机的IP地址列表
QList<QHostAddress> listAddress = hostInfo.addresses();

//获取的主机IP地址列表可能为空,在不为空的情况下使用第一个IP地址
if(!listAddress.isEmpty())
{
    String address = listAddress.at(1).toString();
}

QString detail = "";
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
for(int i = 0; i < interfaces.count(); ++i)
{
    QNetworkInterface interface = interfaces.at(i);
    
    //获取网络接口的名称
    detail += tr("设备:") + interface.name() + "\n";
    detail += tr("硬件地址:") + interface.hardwareAddress() + "\n";
    
    //每个网络接口包括0个或多个IP地址,每个IP地址有选择性地与一个子网掩码和(或)一个广播地址相关联
    QList<QNetworkAddressEntry> addressEntryList = interface.addressEntries();
    for(int j = 1; j < addressEntryList.count(); ++j)
    {
        //QNetworkAddressEntry存储了被网络接口支持的一个IP地址,同时还包括与IP地址相关的子网掩码和广播地址
        QNetworkAddressEntry addressEntry = addressEntryList.at(j);
        detail += "\t" + tr("IP地址:") + addressEntry.ip().toString() + "\n";
        detail += "\t" + tr("子网掩码:") + addressEntry.netmask().toString() + "\n";
        detail += "\t" + tr("广播地址:") + addressEntry.broadcast().toString() + "\n";
    }
}

 

posted on 2022-03-15 15:57  不再低调  阅读(119)  评论(0编辑  收藏  举报