Qt Win7下获取并显示电池电量和WIFI信号强度

    实际上是在Qt里调用Windows库函数实现的,需在头文件中包含“windows.h”和“wlanapi.h”,wlanapi库用于处理无线相关信息,在.pro文件中还需引用wlanapi.lib。

1.获取并显示电池电量

void Widget::showBattery()
{
    SYSTEM_POWER_STATUS systemPowerSatus;
    GetSystemPowerStatus(&systemPowerSatus);
    int remaindPower=(int)systemPowerSatus.BatteryLifePercent;
    if(remaindPower>75)
    {
        QPixmap pixmapBattery(":/icons/BatteryIcon.png");
        ui->labelBattery->setPixmap(pixmapBattery);
    }
    else if(remaindPower>50&&remaindPower<=75)
    {
        QPixmap pixmapBattery(":/icons/BatteryIcon1.png");
        ui->labelBattery->setPixmap(pixmapBattery);
    }
    else if(remaindPower>25&&remaindPower<=50)
    {
        QPixmap pixmapBattery(":/icons/BatteryIcon2.png");
        ui->labelBattery->setPixmap(pixmapBattery);
    }
    else if(remaindPower>0&&remaindPower<=25)
    {
        QPixmap pixmapBattery(":/icons/BatteryIcon3.png");
        ui->labelBattery->setPixmap(pixmapBattery);
    }
    else
    {
        QPixmap pixmapBattery(":/icons/BatteryIcon4.png");
        ui->labelBattery->setPixmap(pixmapBattery);
    }
}
2.获取并显示WIFI信号强度

void Widget::showWIFI()
{
    DWORD dwError = ERROR_SUCCESS;
    DWORD dwNegotiatedVersion;
    HANDLE hClientHandle = NULL;

    dwError = WlanOpenHandle(1, NULL, &dwNegotiatedVersion, &hClientHandle);
    if (dwError != ERROR_SUCCESS)
    {
        WlanCloseHandle(hClientHandle,NULL);
        return;
    }

    PWLAN_INTERFACE_INFO_LIST pInterfaceList = NULL;
    dwError = WlanEnumInterfaces(hClientHandle, NULL,&pInterfaceList);
    if ( dwError != ERROR_SUCCESS )
    {
        WlanFreeMemory(pInterfaceList);
        WlanCloseHandle(hClientHandle,NULL);
        return;
    }

    GUID &guid = pInterfaceList->InterfaceInfo[0].InterfaceGuid;
    PWLAN_AVAILABLE_NETWORK_LIST pWLAN_AVAILABLE_NETWORK_LIST = NULL;

    dwError = WlanGetAvailableNetworkList(hClientHandle, &guid,
        2,NULL, &pWLAN_AVAILABLE_NETWORK_LIST);
    if (dwError != ERROR_SUCCESS)
    {
        WlanFreeMemory(pInterfaceList);
        WlanFreeMemory(pWLAN_AVAILABLE_NETWORK_LIST);
        WlanCloseHandle(hClientHandle,NULL);
        return;
    }

    WLAN_AVAILABLE_NETWORK wlanAN;
    bool isConnected=false;
    int numberOfItems = pWLAN_AVAILABLE_NETWORK_LIST->dwNumberOfItems;
    if (numberOfItems > 0)
    {
        for(int i = 0; i <numberOfItems; i++)
        {
            wlanAN = pWLAN_AVAILABLE_NETWORK_LIST->Network[i];
            if(wlanAN.dwFlags & 1)
            {
                isConnected=true;

                int wifiQuality=(int)wlanAN.wlanSignalQuality;

                if(wifiQuality>75)
                {
                    QPixmap pixmapWireless(":/icons/WirelessIcon.png");
                    ui->labelWireless->setPixmap(pixmapWireless);
                }
                else if(wifiQuality>50&&wifiQuality<=75)
                {
                    QPixmap pixmapWireless(":/icons/WirelessIcon1.png");
                    ui->labelWireless->setPixmap(pixmapWireless);
                }
                else if(wifiQuality>25&&wifiQuality<=50)
                {
                    QPixmap pixmapWireless(":/icons/WirelessIcon2.png");
                    ui->labelWireless->setPixmap(pixmapWireless);
                }
                else if(wifiQuality>0&&wifiQuality<=25)
                {
                    QPixmap pixmapWireless(":/icons/WirelessIcon3.png");
                    ui->labelWireless->setPixmap(pixmapWireless);
                }

            }
        }
    }
    if (!isConnected)
    {
        QPixmap pixmapWireless(":/icons/WirelessIcon4.png");
        ui->labelWireless->setPixmap(pixmapWireless);
    }
    WlanFreeMemory(pInterfaceList);
    WlanFreeMemory(pWLAN_AVAILABLE_NETWORK_LIST);
    WlanCloseHandle(hClientHandle,NULL);
}
显示效果如下图所示

Qt Win7下获取并显示电池电量和WIFI信号强度_第1张图片

源码链接:见http://blog.csdn.net/caoshangpa/article/details/51062351的评论

你可能感兴趣的:(wifi,qt,电池)