侧边栏壁纸
博主头像
三味的小站 博主等级

世界上没有偶然,有的只是必然的结果。

  • 累计撰写 63 篇文章
  • 累计创建 14 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录
Qt

Qt获取本机IP

三味线
2019-04-24 / 0 评论 / 0 点赞 / 18 阅读 / 0 字

获取所有IP

#include <QNetworkInterface>
#include <QHostAddress>
#include <QAbstractSocket>
#include <QDebug>
foreach (QHostAddress addr,QNetworkInterface::allAddresses())
{
    if(addr.protocol()==QAbstractSocket::IPv4Protocol)
    {
        qDebug()<<"IPV4: "<<addr.toString();
    }
    else if(addr.protocol()==QAbstractSocket::IPv6Protocol)
    {
        qDebug()<<"IPV6: "<<addr.toString();
    }
}

输出:

IPV6:  "fe80::e012:9e1d:41c1:14ab%18"
IPV4:  "169.254.20.171"
IPV6:  "fe80::18f8:23c5:f397:3d1a%17"
IPV4:  "169.254.61.26"
IPV6:  "::1"
IPV4:  "127.0.0.1"
IPV6:  "fe80::7d9e:2612:9d35:4012%11"
IPV4:  "192.168.2.120"
IPV6:  "::1"
IPV4:  "127.0.0.1"
IPV6:  "fe80::100:7f:fffe%12"
IPV6:  "fe80::5efe:c0a8:278%14"

获取局域网IP

#include <QHostAddress>
#include <QHostInfo>
#include <QAbstractSocket>
#include <QDebug>
QHostInfo info=QHostInfo::fromName(QHostInfo::localHostName());
foreach(QHostAddress address, info.addresses())
{
    if (address.protocol() == QAbstractSocket::IPv4Protocol)
    {
        qDebug() << "IPv4 Address:" << address.toString();
    }
}

输出:

IPv4 Address: "192.168.2.120"

0

评论区