应用初始化本地数据,需要读取配置文件,小编介绍如何通过xml简单写入配置文件,至于为什么选择这个格式,主要因为在qml中有一个很好
的类使用:XmlListModel
xmlListModel能直接读取xml配置文件,快速方便开发,再加上自己写一个专门写入配置文件的类,两个就成了绝配啊!
代码:
datasbase.h
#ifndef DATASBASE_H
#define DATASBASE_H
#include
#include
#include
#include
#include
class DatasBase : public QObject
{
Q_OBJECT
public:
explicit DatasBase(QObject *parent = 0);
~DatasBase();
Q_INVOKABLE void setFileName(QString filename);
Q_INVOKABLE void writeStartDocument();
Q_INVOKABLE void writeStartElement(QString str);
Q_INVOKABLE void writeTextElement(QString elementT, QString content);
Q_INVOKABLE void writeEndElement();
Q_INVOKABLE void writeEndDocument();
Q_INVOKABLE bool openplaylistFile();
Q_INVOKABLE void closeplaylistFile();
signals:
public slots:
private:
QFile *playListDatas;
QDir *playListDatasdir;
QXmlStreamWriter *xmlWriter;
};
#endif // DATASBASE_H
datasbase.cpp
#include "datasbase.h"
#include
DatasBase::DatasBase(QObject *parent) : QObject(parent)
{
//"file:///mnt/android/data/colorlight/playlist->xml"
//配置文件的路径
playListDatasdir=new QDir("/mnt/sdcard/Android/data/colorlight/playlist.xml");
//判断是否存在文件
if(!playListDatasdir->exists()){
qDebug()<mkdir("/mnt/sdcard/Android/data/colorlight");
}
//初始化配置数据
playListDatas=new QFile("/mnt/sdcard/Android/data/colorlight/playlist.xml");
xmlWriter=new QXmlStreamWriter(playListDatas);
xmlWriter->setAutoFormatting(true);
qDebug()<absolutePath();
}
DatasBase::~DatasBase()
{
}
//设置配置文件的文件名
void DatasBase::setFileName(QString filename){
playListDatas->setFileName(filename);
}
bool DatasBase::openplaylistFile(){
return playListDatas->open(QIODevice::WriteOnly | QIODevice::Text);
}
void DatasBase::writeStartDocument(){
xmlWriter->writeStartDocument();
}
void DatasBase::writeStartElement(QString str){
xmlWriter->writeStartElement(str);
}
void DatasBase::writeTextElement(QString elementT, QString content){
xmlWriter->writeTextElement(elementT,content);
}
void DatasBase::writeEndElement(){
xmlWriter->writeEndElement();
}
void DatasBase::writeEndDocument(){
xmlWriter->writeEndDocument();
}
void DatasBase::closeplaylistFile(){
playListDatas->close();
}
通过这个类,我们可以方便在qml中调用写入数据配置的文件
下面是写入音乐列表配置的代码例子
XmlListModel{
id:playlistModel
source:"file:///mnt/sdcard/Android/data/colorlight/playlist.xml"
query: "/data/music"
XmlRole{
name:"name";query: "name/string()";isKey: true
}
XmlRole{
name:"path";query: "path/string()";isKey: true
}
onStatusChanged: {
if(status==XmlListModel.Ready){
for(var mindex=0;mindex
tempplaylistdata.append({"name":playlistModel.get(mindex).name,
"path":playlistModel.get(mindex).path})
}
}
}
}
ListModel{
id:tempplaylistdata
}
//保存扫描后的音乐文件
function saveDatas(){
if(tempplaylistdata.count==0){
return
}
// console.log()
datasbase.openplaylistFile()
datasbase.writeStartDocument()
datasbase.writeStartElement("data")
for(var a=0;a
datasbase.writeStartElement("music")
datasbase.writeTextElement("name",tempplaylistdata.get(a).name)
datasbase.writeTextElement("path",tempplaylistdata.get(a).path)
datasbase.writeEndElement()
}
datasbase.writeEndElement()
datasbase.writeEndDocument()
datasbase.closeplaylistFile()
}