Qt网络编程:获取本地Ip地址:
-
- QList<QHostAddress> addList = QNetworkInterface::allAddresses();
-
- foreach(QHostAddress address,addList)
- {
-
- if(address.protocol() == QAbstractSocket::IPv4Protocol
- && address != QHostAddress(QHostAddress::LocalHost))
- {
-
- qDebug() << address.toString();
- }
- }
- qDebug() << "----------------------------------";
-
-
- QList<QNetworkInterface> interface = QNetworkInterface::allInterfaces();
-
- foreach(QNetworkInterface inter,interface)
- {
- QList<QNetworkAddressEntry> entrList = inter.addressEntries();
-
- foreach(QNetworkAddressEntry entr,entrList)
- {
- QHostAddress hostIp = entr.ip();
- if(hostIp.protocol() == QAbstractSocket::IPv4Protocol &&
- hostIp != QHostAddress(QHostAddress::LocalHost))
- {
-
- qDebug()<<QString("名称:%1").arg(inter.humanReadableName());
-
- qDebug() << QString("HostIp:%1").arg(hostIp.toString());
-
- qDebug() << QString("NetMask:%1").arg(entr.netmask().toString());
-
- qDebug() << QString("HardAddress:%1").arg(inter.hardwareAddress());
- qDebug() << "----------------------------------";
- }
- }
-
- }
一般结合下拉框使用
本文转自:
http://blog.csdn.net/jiezhj/article/details/37511603