【Qt 应用】模仿实现Win10的Wifi列表

这里使用 Qt 模仿实现了 Win10 系统下的 Wifi 列表,主要用的是 QlistWidget + xml + cmd命令行 实现。

效果

QtApp_wifiList.gif

下载地址

https://github.com/confidentFeng/QtAppProject

关键代码

// 下一步按钮
connect(m_pBtnNext, &QPushButton::clicked, [=]{
    // 1. 创建wifi配置文件
    QString strWifiName = m_pLabName->text();
    XmlHelper::Get()->WriteXml(strWifiName, m_pEditPasswd->text());
    // 2. 添加wifi配置文件
    QString strReturn = AppCommon::Get().ExecuteCmd(QString("netsh wlan add profile filename=%1/%2.xml")\
                                                    .arg(QApplication::applicationDirPath()).arg(strWifiName));
    if (strReturn.contains("已将配置文件"))
    {
        // 密码正确,但未连接成功,则设置为连接中状态
        this->setState(Connecting);
        // 延时1.5秒,以显示加载状态
        AppCommon::Get().sleepEvent(1500);

        // 连接wifi
        QString strConReturn = AppCommon::Get().ExecuteCmd(QString("netsh wlan connect name=%1").arg(strWifiName));
        qDebug() << "reurn " << strConReturn;
        if(strConReturn.contains("已成功完成连接请求")) {
            // 连接成功,则设置为连接中状态
            this->setState(Connected);
        }
        else {
            // 密码错误,未连接成功,则设置为编辑状态
            this->setState(Edit);

            m_pLabErrorTip->setText(tr("网络安全秘钥不正确。请再试一次。"));
            m_pLabErrorTip->show(); // 错误提示
        }
    }
    else if (strReturn.contains("配置文件格式错误"))
    {
        m_pLabErrorTip->setText(tr("网络安全秘钥不正确。请再试一次。"));
        m_pLabErrorTip->show(); // 错误提示
    }

    // 不管添加wifi配置文件是否成功,都删除相应配置文件
    QString fileName = QApplication::applicationDirPath() + "/" + strWifiName + ".xml";
    QFile fileTemp(fileName);
    fileTemp.remove();
});

// 写xml:WIFI的profile脚本文本
void XmlHelper::WriteXml(const QString& strWifiName, const QString& strWifiPasswd)
{
    // 打开或创建文件
    QFile file(QApplication::applicationDirPath() + "/" + strWifiName + ".xml"); // 相对路径、绝对路径、资源路径都可以
    if(!file.open(QFile::WriteOnly | QFile::Truncate)) // 可以用QIODevice,Truncate表示清空原来的内容
        return;

    QDomDocument doc;
    // 写入xml头部
    QDomProcessingInstruction instruction; // 添加处理命令
    instruction = doc.createProcessingInstruction("xml", "version=\"1.0\"");
    doc.appendChild(instruction);
    // 添加根节点
    QDomElement root = doc.createElement("WLANProfile");
    root.setAttribute("xmlns", "http://www.microsoft.com/networking/WLAN/profile/v1");
    doc.appendChild(root);

    // 添加子元素-name
    QDomElement name = doc.createElement("name"); // 创建子元素
    QDomText text;
    text = doc.createTextNode(strWifiName);
    name.appendChild(text); //添加子元素值
    root.appendChild(name); // 添加子元素

    // 添加子元素-SSIDConfig
    QDomElement SSIDConfig = doc.createElement("SSIDConfig");
    QDomElement SSID = doc.createElement("SSID");
    QDomElement SSID_hex = doc.createElement("hex");
    QDomElement SSID_name = doc.createElement("name");
    //SSID_hex.appendChild(doc.createTextNode("4445565F322E3447")); // DEV_24.G 8个
	//SSID_hex.appendChild(doc.createTextNode("52454D4F54452D352E3047")); // REMOTE-5.0G 11个
	SSID_hex.appendChild(doc.createTextNode(QString(strWifiName.toLatin1().toHex())));
    SSID_name.appendChild(doc.createTextNode(strWifiName));
    SSID.appendChild(SSID_hex);
    SSID.appendChild(SSID_name);
    SSIDConfig.appendChild(SSID);
    root.appendChild(SSIDConfig);

    // 添加子元素-connectionType
    QDomElement connectionType = doc.createElement("connectionType");
    connectionType.appendChild(doc.createTextNode("ESS"));
    root.appendChild(connectionType);

    // 添加子元素-connectionMode
    QDomElement connectionMode = doc.createElement("connectionMode");
    connectionMode.appendChild(doc.createTextNode("auto"));
    root.appendChild(connectionMode);

    // 添加子元素-MSM
    QDomElement MSM = doc.createElement("MSM");
    QDomElement security = doc.createElement("security");
    // security添加子元素-authEncryption
    QDomElement authEncryption = doc.createElement("authEncryption");
    QDomElement authentication = doc.createElement("authentication");
    QDomElement encryption = doc.createElement("encryption");
    QDomElement useOneX = doc.createElement("useOneX");
    authentication.appendChild(doc.createTextNode("WPA2PSK"));
	//authentication.appendChild(doc.createTextNode("WPAPSK"));
    encryption.appendChild(doc.createTextNode("AES"));
    useOneX.appendChild(doc.createTextNode("false"));
    authEncryption.appendChild(authentication);
    authEncryption.appendChild(encryption);
    authEncryption.appendChild(useOneX);
    security.appendChild(authEncryption);

    // security添加子元素-sharedKey
    QDomElement sharedKey = doc.createElement("sharedKey");
    QDomElement keyType = doc.createElement("keyType");
    QDomElement protectedP = doc.createElement("protected");
    QDomElement keyMaterial = doc.createElement("keyMaterial");
    keyType.appendChild(doc.createTextNode("passPhrase"));
    protectedP.appendChild(doc.createTextNode("false"));
    keyMaterial.appendChild(doc.createTextNode(strWifiPasswd));
    sharedKey.appendChild(keyType);
    sharedKey.appendChild(protectedP);
    sharedKey.appendChild(keyMaterial);
    security.appendChild(sharedKey);
    // MSM添加子元素-security
    MSM.appendChild(security);
    root.appendChild(MSM);

    // 添加子元素-MSM
    QDomElement MacRandomization = doc.createElement("MacRandomization");
    MacRandomization.setAttribute("xmlns", "http://www.microsoft.com/networking/WLAN/profile/v3");
    QDomElement enableRandomization = doc.createElement("enableRandomization");
    enableRandomization.appendChild(doc.createTextNode("false"));
    MacRandomization.appendChild(enableRandomization);
    root.appendChild(MacRandomization);

    // 输出到文件
    QTextStream out_stream(&file);
    doc.save(out_stream,4); // 缩进4格
    file.close();
}

posted @ 2023-07-11 10:22  fengMisaka  阅读(56)  评论(0编辑  收藏  举报