现在经常的使用qt,现在需要解决使用qt如何获取本机的IP地址,经过摸索终于找到了方法。
- QHostAddress CommonHelper::getLocalHostIP()
-
{
-
QList<QHostAddress> AddressList = QNetworkInterface::allAddresses();
-
QHostAddress result;
-
foreach(QHostAddress address, AddressList){
-
if(address.protocol() == QAbstractSocket::IPv4Protocol &&
-
address != QHostAddress::Null &&
-
address != QHostAddress::LocalHost){
-
if (address.toString().contains("127.0.")){
-
continue;
-
}
-
result = address;
-
break;
-
}
-
}
-
return result;
-
}
阅读(1100) | 评论(0) | 转发(0) |