qt get local ip address code example
Example: qt qet name of PC and ipaddres
QString localhostname = QHostInfo::localHostName();
QString localhostIP;
QList<QHostAddress> 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;