Qt编程14:Qt获取本地网络IP地址以及其他属性(以IPv4为例)

Qt网络编程:获取本地Ip地址:

 

    //获取本地IP片地址,
    QList addList = QNetworkInterface::allAddresses();

    foreach(QHostAddress address,addList)
    {
        //排除IPV6,排除回环地址
        if(address.protocol() == QAbstractSocket::IPv4Protocol
                && address != QHostAddress(QHostAddress::LocalHost))
        {
            //输出,转换为字符串格式
            qDebug() << address.toString();
        }
    }
    qDebug() << "----------------------------------";

    //获取本地所有的网络接口
    QList interface = QNetworkInterface::allInterfaces();

    foreach(QNetworkInterface inter,interface)
    {
        QList entrList = inter.addressEntries();

        foreach(QNetworkAddressEntry entr,entrList)
        {
            QHostAddress hostIp = entr.ip();
            if(hostIp.protocol() == QAbstractSocket::IPv4Protocol &&
                    hostIp != QHostAddress(QHostAddress::LocalHost))
            {
                //获取输出名称
                qDebug()<

一般结合下拉框使用

 Qt编程14:Qt获取本地网络IP地址以及其他属性(以IPv4为例)_第1张图片

 

 

你可能感兴趣的:(Qt基本应用,Qt网络编程)