應用初始化本地數據,須要讀取配置文件,小編介紹如何經過xml簡單寫入配置文件,至於爲何選擇這個格式,主要由於在qml中有一個很好
的類使用:XmlListModel
xmlListModel能直接讀取xml配置文件,快速方便開發,再加上本身寫一個專門寫入配置文件的類,兩個就成了絕配啊!
代碼:
datasbase.hcss
#ifndef DATASBASE_H #define DATASBASE_H #include <QObject> #include <QXmlStreamWriter> #include <QFile> #include <QDir> #include <QString> 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.cppandroid
#include "datasbase.h" #include <QDebug> 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**************"<< playListDatasdir->mkdir("/mnt/sdcard/Android/data/colorlight"); } //初始化配置數據 playListDatas=new QFile("/mnt/sdcard/Android/data/colorlight/playlist.xml"); xmlWriter=new QXmlStreamWriter(playListDatas); xmlWriter->setAutoFormatting(true); qDebug()<<"path"<<playListDatasdir->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中調用寫入數據配置的文件
下面是寫入音樂列表配置的代碼例子c++
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<playlistModel;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<tempplaylistdata.count;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() }