PDA

View Full Version : QHostInfo IP Detection under Vista



visor_ua
4th May 2008, 11:20
Next code is woring well under all win system before vista.

QString ipAddress;
QHostInfo info = QHostInfo::fromName( QHostInfo::localHostName() );
QList<QHostAddress> listAddr= info.addresses();
ipAddress = listAddr.first().toString();
Under vista this code giving IPv6 addr. (FE80:0:0:0:4B1:.....)
Any ideas?

marcel
4th May 2008, 14:34
Try to convert it to ipv4 with QHostInfo::toIPb4Address.
You should construct a new QHostInfo from that value.

To make it work on both vista and xp you can test the protocol with QHostInfo::protocol and make the necessary conversion, if necessary.

visor_ua
4th May 2008, 17:04
Yes. Solved using protocol() method

QHostInfo info = QHostInfo::fromName( QHostInfo::localHostName() );
QList<QHostAddress> listAddr= info.addresses();
for( int i = 0; i < listAddr.size(); i ++ )
{
if( listAddr.at(i).protocol() == QAbstractSocket::IPv4Protocol )
{
ipAddress = listAddr.at(i).toString();
break;
}
}