1、Config.h



#ifndef CONFIG_H
#define CONFIG_H
#include <QString>
#include <QFile>
#include <QDebug>
#include <QDomDocument>

class Config
{
public:
/*获取单例*/
static Config& getInstance();
/*获取文件夹目录*/
static QString getFilePath();
private:
Config();
static QDomElement m_element; //xml对象
const QString XML_PATH = "/cfg/cfg.xml"; //xml路径
};

#endif // CONFIG_H


2、Config.cpp



#include "config.h"
#include <QDir>
Config::Config()
{
QString pathStr = QDir::currentPath() + XML_PATH;
QDomDocument doc;
QFile file(pathStr);
if(file.open(QFile::ReadOnly)){
if(doc.setContent(&file)){
m_element = doc.documentElement(); //返回根节点
}
file.close();
}else{
qDebug() << "xml file open faild.";
}
}


QDomElement Config::m_element;
Config& Config::getInstance()
{
static Config instance;
return instance;
}

QString Config::getFilePath()
{
QString ret;
for(int stepIndex = 0; stepIndex < m_element.childNodes().size(); stepIndex++) {
QDomElement oneElment = m_element.childNodes().at(stepIndex).toElement();
if("quality_file_path" == oneElment.tagName()){
ret = oneElment.text();
break;
}
}
return ret;
}


Qt操作xml_#ifndef

 




长风破浪会有时,直挂云帆济沧海!