首页| 论坛| 消息
主题:很简单的代码,但为何Qt给出如此奇怪的字符串?
回帖:listAddress.at(2)换成listAddress.at(0)或者1试试,你这直接就显示后面的干嘛?你可以过滤啊,这种一般是IPV6的地址。
QStringList OnvifHelper::getLocalIps()
{
QStringList ips;
QList netInterfaces = QNetworkInterface::allInterfaces();
foreach (const QNetworkInterface&netInterface, netInterfaces) {
//移除虚拟机和抓包工具的虚拟网卡
QString humanReadableName = netInterface.humanReadableName().toLower();
if (humanReadableName.startsWith("vmware network adapter") || humanReadableName.startsWith("npcap loopback adapter")) {
continue;
}
//过滤当前网络接口
bool flag = (netInterface.flags() == (QNetworkInterface::IsUp | QNetworkInterface::IsRunning | QNetworkInterface::CanBroadcast | QNetworkInterface::CanMulticast));
if (flag) {
QList addrs = netInterface.addressEntries();
foreach (QNetworkAddressEntry addr, addrs) {
//只取出IPV4的地址
if (addr.ip().protocol() == QAbstractSocket::IPv4Protocol) {
QString ip4 = addr.ip().toString();
if (ip4 != "127.0.0.1") {
ips.append(ip4);
}
}
}
}
}
return ips;
}
下一楼›:那是IPV6的地址,你应该抓取IPV4的,楼上有正解!
‹上一楼:那是ip6返回结果
quint32nIp4 = addr.toIPv4Address();
QHostAddress host_addr = QHost ..

查看全部回帖(6)
«返回主帖