#include <QtDebug>
#include <QHostAddress>
#include <QNetworkInterface>
#include <QTextCodec>
GetIpv4::GetIpv4()
{
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QList<QHostAddress> vAddressList = QNetworkInterface::allAddresses();
for(int i=0;i<vAddressList.size();i++)
{
qDebug()<<vAddressList.at(i);
if((vAddressList.at(i)!=QHostAddress::LocalHost)&&(vAddressList.at(i).protocol()==QAbstractSocket::IPv4Protocol))
qDebug()<<QString("本机IP地址是:")<<vAddressList.at(i).toString();
}
}
以上是代码:
以下是运行结果:
QHostAddress( "127.0.0.1" )
QHostAddress( "0:0:0:0:0:0:0:1" )
QHostAddress( "192.168.0.105" )
"本机IP地址是:" "192.168.0.105"
QHostAddress( "FE80:0:0:0:20C:29FF:FE1B:6F25" )