Qt下如何獲得本地網路卡IP

alan00000發表於2012-12-29
#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" ) 

相關文章