void NetworkInformation::getHostInformation()
{
QString localHostName = QHostInfo::localHostName(); //(a)
LineEditLocalHostName->setText(localHostName);
QHostInfo hostInfo = QHostInfo::fromName(localHostName); //(b)
//获得主机的IP地址列表
QList<QHostAddress> listAddress = hostInfo.addresses();
if(!listAddress.isEmpty()) //(c)
{
LineEditAddress->setText(listAddress.at(2).toString());
}
}
void NetworkInformation::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("device:")+interface.name()+"\n";
//(b)
detail=detail+tr("Mac:")+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("mask:")+entry.netmask().toString() +"\n";
detail=detail+"\t"+tr("broadcast:")+entry.broadcast().toString() +"\n";
}
}
QMessageBox::information(this,tr("Detail"),detail);
}
02-03
1090
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-12
1275
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)