I'm new to Qt and QML. With that said, I am making a editable list in QML which I would like to import and export as XML file. Right now I'm stuck on importing it from XML file and setting it as my ListView
's model from the C++ side.
I'm hoping I can transform the XML to a form
I want to be able to add, remove and edit rows in my ListView from the QML side, so using XmlListModel
looks like a bad idea, i.e. it doesn't offer those abilities.
my main.qml in which I am choosing the file location:
FileDialog {
id: exportDialog
title: "Please choose an XML TV file"
nameFilters: [("*.xml")]
onAccepted: {
fileio.parse(importDialog.fileUrl)
}
onRejected: {
console.log("Canceled")
}
}
My C++ header file in which I would like the conversion to happen:
#ifndef FILEIO_H
#define FILEIO_H
#include <QObject>
#include <QFile>
#include <QTextStream>
#include <QXmlStreamReader>
#include <QDebug>
class FileIO : public QObject
{
Q_OBJECT
public slots:
bool parse(const QString& source)
{
if (source.isEmpty())
return false;
QFile file(source);
if (!file.open(QFile::WriteOnly | QFile::Truncate))
return false;
QFile* mjau = new QFile(source);
if (!mjau->open(QIODevice::ReadOnly | QIODevice::Text)) {
printf("Load XML File Problem");
return false;
}
QXmlStreamReader reader(mjau);
while(!reader.atEnd() && !reader.hasError()) {
if(reader.readNext() == QXmlStreamReader::StartElement && reader.name() == "parent") {
qDebug() << reader.readElementText();
//printf(reader.readElementText())
}
}
}
public:
FileIO() {}
};
#endif // FILEIO_H
Example of xml file I'd like to parse, multitude of entries:
<notes>
<note>
<heading>Help</heading>
<body>I want to make this work!</body>
</note>
<note>
<heading>Because</heading>
<body>I love QML <3</body>
</note>
...
</notes>
To something like this:
ListModel {
id: notes
ListElement {
heading: "Help"
body: "I want to make this work!"
}
ListElement {
heading: "Because"
body: "I love QML <3"
}
}
Right now, when I choose the XML file nothing happens, but that same file is "cleaned out", becomes a blank .xml file.
How could I parse it properly, and after that, convert the parsed data into QML's ListView ListModel
or any model that can support editing from the QML's side?
--UPDATE-- Parsing of the XML file will be a one-time procedure, after the parse I intend to work with the data in javascript, so it would be cool if the data could be parsed in JSON object structure.