Qt 查看本地所有IP地址
void MyNetwork::checkNetwork()
{
bool ethernetIpOk = false;
bool wlanIpOk = false;
QList<QNetworkInterface> networkInterfaces = QNetworkInterface::allInterfaces();
for (int i = 0; i < networkInterfaces.size(); i++)
{
QString hardwareAddress = networkInterfaces[i].hardwareAddress();
QString readableName = networkInterfaces[i].humanReadableName();
QString interfaceName = networkInterfaces[i].interfaceNameFromIndex(i);
QString adapterName = networkInterfaces[i].name();
QList<QNetworkAddressEntry> addressEntryList = networkInterfaces[i].addressEntries();
for (int j = 0; j < addressEntryList.size(); j++)
{
QString ipAddress = addressEntryList[j].ip().toString();
QString ipMask = addressEntryList[j].netmask().toString();
if (adapterName.contains("ethernet"))
{
QStringList ipList = ipAddress.split('.');
if (ipList.size() < 4)
continue;
if (ipAddress.contains("192.168.0"))
{
ethernetIpOk = true;
sigMsg(tr("Info: Locl ethernet ip is ") + ipAddress);
}
}
else if (adapterName.contains("wireless"))
{
QStringList ipList = ipAddress.split('.');
if (ipList.size() < 4)
continue;
if (ipAddress.contains("192.168.0"))
{
wlanIpOk = true;
sigMsg(tr("Info: Locl wireless ip is ") + ipAddress);
}
}
}
}
if (!ethernetIpOk)
{
sigMsg(tr("Warning: Wired IP address isn't in range of 192.168.0.xxx."));
}
if (!wlanIpOk)
{
sigMsg(tr("Warning: Wireless IP address isn't in range of 192.168.0.xxx."));
}
}