• QT读取xml配置文件


    //获取字符串字段
    QString ConfigHelper::GetStringConfigValue(QString str) {
    
        if(str == "InitDeviceNo")
        {
            return getMacPath();
        }else
        {
            QString filePath=QString("%1/%2").arg( qApp->applicationDirPath()).arg("config.xml");
            QString names=QString::null;
            ////打开或创建文件
            QFile file(filePath); //相对路径、绝对路径、资源路径都行
            if(!file.open(QFile::ReadOnly))
            {
    
            }
            QDomDocument doc;
            if(!doc.setContent(&file))
            {
                file.close();
            }
            file.close();
            QDomElement root=doc.documentElement(); //返回根节点
            QDomNode node=root.firstChild(); //获得第一个子节点
            while(!node.isNull())  //如果节点不空
            {
                if(node.isElement()) //如果节点是元素
                {
    
                    QDomElement e=node.toElement(); //转换为元素,注意元素和节点是两个数据结构,其实差不多
                    if(e.attribute("key")==str)
                    {
    
                        names=e.attribute("value");
                       break;
                    }
    
                }
                node=node.nextSibling(); //下一个兄弟节点,nextSiblingElement()是下一个兄弟元素,都差不多
            }
            return names;
        }
    }
  • 相关阅读:
    C语言之回调函数&模块化
    680. 验证回文字符串 Ⅱ
    C++指针数组和数组指针
    345. 反转字符串中的元音字母
    633.平方数之和
    IDM使用介绍篇
    路由器无线桥接WDS
    约数的个数
    密码翻译
    查找学生信息
  • 原文地址:https://www.cnblogs.com/xupeidong/p/10332929.html
Copyright © 2020-2023  润新知