获取本地IP地址,排除127回环:
QString MainWindow::getLocalIP()
{
QList<QHostAddress> list = QNetworkInterface::allAddresses();
foreach (QHostAddress address, list)
{
//使用IPv4地址
if(address.protocol() == QAbstractSocket::IPv4Protocol)
{
if (address.toString().contains("127.0."))
{
//qDebug()<<address.toString();
continue;
}
else
{
//qDebug()<<address.toString();
return address.toString();
}
}
}
return 0;
}