Qt网络编程:获取本地Ip地址:
//获取本地IP片地址,
QList addList = QNetworkInterface::allAddresses();
foreach(QHostAddress address,addList)
{
//排除IPV6,排除回环地址
if(address.protocol() == QAbstractSocket::IPv4Protocol
&& address != QHostAddress(QHostAddress::LocalHost))
{
//输出,转换为字符串格式
qDebug() << address.toString();
}
}
qDebug() << "----------------------------------";
//获取本地所有的网络接口
QList interface = QNetworkInterface::allInterfaces();
foreach(QNetworkInterface inter,interface)
{
QList entrList = inter.addressEntries();
foreach(QNetworkAddressEntry entr,entrList)
{
QHostAddress hostIp = entr.ip();
if(hostIp.protocol() == QAbstractSocket::IPv4Protocol &&
hostIp != QHostAddress(QHostAddress::LocalHost))
{
//获取输出名称
qDebug()<
//获取输出ip地址
qDebug() << QString("HostIp:%1").arg(hostIp.toString());
//获取输出子网掩码
qDebug() << QString("NetMask:%1").arg(entr.netmask().toString());
//获取并输出mac地址
qDebug() << QString("HardAddress:%1").arg(inter.hardwareAddress());
qDebug() << "----------------------------------";
}
}
}
一般结合下拉框使用