Qt获取本机IPV4 IP地址

挺简单的代码,直接看吧
#include 
   
   
    
    
#include 
    
    
     
     

/*************************************  
* 获取本机 IPV4 地址,如果有多个,返回第
* 一个有效的IPV4地址
**************************************/
QHostAddress getHostIPV4Address()
{
	foreach(const QHostAddress& hostAddress,QNetworkInterface::allAddresses())
		if ( hostAddress != QHostAddress::LocalHost && hostAddress.toIPv4Address() )
			return hostAddress;

	return QHostAddress::LocalHost;
}
    
    
   
   

你可能感兴趣的:(Qt)