获取所有IP#include <QNetworkInterface> #include <QHostAddress> #include <QAbstractSocket> #include <QDebug> foreach (QHostAddress addr,QNetworkInterface::allAddresses()) { if(addr.protocol()==QAbstractSocket::IPv4Protocol) { qDebug()<<"IPV4: "<<addr.toString(); } else if(addr.protocol()==QAbstractSocket::IPv6Protocol) { qDebug()<<"IPV6: "<<addr.toString(); } }输出:IPV6: "fe80::e012:9e1d:41c1:14ab%18" IPV4: "169.254.20.171" IPV6: "fe80::18f8:23c5:f397:3d1a%17" IPV4: "169.254.61.26" IPV6: "::1" IPV4: "127.0.0.1" IPV6: "fe80::7d9e:2612:9d35:4012%11" IPV4: "192.168.2.120" IPV6: "::1" IPV4: "127.0.0.1" IPV6: "fe80::100:7f:fffe%12" IPV6: "fe80::5efe:c0a8:278%14"获取局域网IP#include <QHostAddress> #include <QHostInfo> #include <QAbstractSocket> #include <QDebug> QHostInfo info=QHostInfo::fromName(QHostInfo::localHostName()); foreach(QHostAddress address, info.addresses()) { if (address.protocol() == QAbstractSocket::IPv4Protocol) { qDebug() << "IPv4 Address:" << address.toString(); } }输出:IPv4 Address: "192.168.2.120"
评论区