📅  最后修改于: 2022-03-11 14:55:32.148000             🧑  作者: Mango
QString localhostname = QHostInfo::localHostName();
QString localhostIP;
QList hostList = QHostInfo::fromName(localhostname).addresses();
foreach (const QHostAddress& address, hostList) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address.isLoopback() == false) {
localhostIP = address.toString();
}
}
QString localMacAddress;
QString localNetmask;
foreach (const QNetworkInterface& networkInterface, QNetworkInterface::allInterfaces()) {
foreach (const QNetworkAddressEntry& entry, networkInterface.addressEntries()) {
if (entry.ip().toString() == localhostIP) {
localMacAddress = networkInterface.hardwareAddress();
localNetmask = entry.netmask().toString();
break;
}
}
}
qDebug() << "Localhost name: " << localhostname;
qDebug() << "IP = " << localhostIP;
qDebug() << "MAC = " << localMacAddress;
qDebug() << "Netmask = " << localNetmask;