Write directory title here
reference
Qt network and communication (obtain network information such as local IP, MAC, IPV6 subnet mask) + source code
main code
QString strHostName = QHostInfo::localHostName();
QString strHostDomain = QHostInfo::localDomainName();
qInfo() <<tr("本地域名:")+strHostDomain;
m_ptrLineEditHostName->setText(strHostName);
QHostInfo hostInfo = QHostInfo::fromName(strHostName);
qInfo() <<tr("addresses:");
QList<QHostAddress> list = hostInfo.addresses();
foreach (auto item, list) {
qInfo() <<item.toString();
}
qInfo() <<tr("----------------------------------------------------------");
qInfo() <<tr("listAddress:");
QList<QHostAddress> listAddress = QNetworkInterface::allAddresses();
foreach (auto item, listAddress) {
qInfo() <<item.toString();
}
QList<QNetworkInterface> listNetworkInterface=QNetworkInterface::allInterfaces();
qInfo() <<tr("----------------------------------------------------------");
foreach (auto item, listNetworkInterface) {
qInfo() << tr("网口设备:") + item.name();
qInfo() << tr("硬件地址:") + item.hardwareAddress();
foreach (auto item2, item.addressEntries()) {
qInfo() << " " + tr("ip地址:") + item2.ip().toString();
qInfo() << " " + tr("子网掩码:") + item2.netmask().toString();
qInfo() << " " + tr("广播地址:") + item2.broadcast().toString();
}
}
Effect
![insert image description here](https://img-blog.csdnimg.cn/04d44ca4617b496d822abb6cf7c999da.png)