qt android 开发之写入ml配置文件篇
应用初始化本地数据,需要读取配置文件,小编介绍如何通过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
关键字:qt5, #c++#
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!