PDA

View Full Version : How to set an .xml file as datasource to Listview



Mathan
1st October 2016, 12:28
Hi,

I am writing an rexlog.xml will be the data source to Listview in main.qml. But, The listview is not showing the value. In the xml the values are written and it is accessable.

Query: I suspect, there will be performance issue, reading from .xml file and loading to Listview, Instead of that Is it good to load the data initially to QList or any similiar, so the listview can easily fetch the data for many events like scroll up/down.

Which option will be better? The xml entry can be atleast 80-100 set of data.

Code snippets:


main.qml
-----------


property string strXMLPath: System.userHomePath + "/rexlog.xml" //.xml file stored in the c:\user\proile\rexlog.xml


XmlListModel {
id: idXMLListModel
source: strXMLPath
query: "/catalog/book"
XmlRole {name: "title"; query: "title/string()"}

XmlRole { name: "first_author"; query: "author[1]/string()" }
XmlRole { name: "year"; query: "year/number()" }
}


//Note: Add the click event for ListView
ListView {
anchors {
left: parent.left
leftMargin: 10 * scaleFactor
right: parent.right
rightMargin: 10 * scaleFactor
top: rectangleToolBar.bottom
topMargin: 10 * scaleFactor
bottom: rectangleStatusBar.top
bottomMargin: 10 * scaleFactor
}

model: idXMLListModel
delegate: Column {
Layout.alignment: Qt.AlignCenter
Text { text: title + " (" + type + ")"; font.bold: wanted }
Text { text: first_author }
Text { text: year }
}
}

Component {
id: comsearchDelegate

Row {
spacing: 10 * scaleFactor

Image {
id: imageItem
width: 100 * scaleFactor
height: 70 * scaleFactor
source: thumbnailUrl
}

Column {
Layout.alignment: Qt.AlignTop
Text { text: Title; font { pixelSize: 14 * scaleFactor; bold: true } }

}
}
}


.xml
-----


<?xml version="1.0" encoding="ISO-8859-1"?>

-<catalog>


-<book wanted="true" type="Online">

<title>Qt 5 Cadaques</title>

<year>2014</year>

<author>Juergen Bocklage-Ryannel</author>

<author>Johan Thelin</author>

</book>


-<book type="Hardcover">

<title>C++ GUI Programming with Qt 4</title>

<year>2006</year>

<author>Jasmin Blanchette</author>

<author>Mark Summerfield</author>

</book>


-<book type="Paperback">

<title>Programming with Qt</title>

<year>2002</year>

<author>Matthias Kalle Dalheimer</author>

</book>

</catalog>

anda_skoa
1st October 2016, 16:12
If you have XML data then the XmlListModel is quite efficient in displaying it.

If you have the data in a different data structure and write it to XML just to have it displayed then creating a list or custom model would likely be better.

Regarding the XML not loading, what you have debugged so far?
What's the model's status?
Also, what do you have these "-" in there for?

Cheers,
_

d_stranz
1st October 2016, 17:37
Also, what do you have these "-" in there for?

If those are actually in the XML, then "-<book ...>" is not valid XML and the XMLListModel is probably rejecting it. The only place that extra text characters is allowed in valid XML is within elements; eg. between "<book>" and "</book>" tags. These "-" characters are between tags, and thus makes the XML invalid.

Mathan
3rd October 2016, 15:26
Hi,

I tried according to your instructions. The below xml is properly read by the listView. But at the same time, I stored in the C:\userprofile\reg.xml, Then the value is not readable.


Working Code Snippet:

XmlListModel {
id: idXMLListModel
xml: "<Download><Maps><Title>MINUSCAOffice</Title><Title>MINUSCAOffice2</Title></Maps></Download>" //This xml working properly.
query: "/Download/Maps/Title"
XmlRole {
name: "Title"
query: "string()"
}
}

Not working xml: //The Dash and xml header are added by itself.
<?xml version="1.0"?>
-<Download>
-<Maps>
<Title>MINUSCAOffice</Title>
<Title>MINUSCAOffice2</Title>
</Maps>
</Download>

Query: I had created the reg.xml when the first time login authenticated and downloaded items details are stored in xml. So I can read the reg.xml values in Readxml.CPP and just want to pass it to ListView, B'cos binding the xmlListmodel to listview brings trouble like mentioned earlier. Is any way to pass the value from Readxml.cpp to main.qml listview like passing as arguments?

Ex:
ListView {
id: idListView
anchors.fill: parent

model: objReadxml.model() //What will be the argument type?
delegate: comsearchDelegate
spacing: 10 * scaleFactor
clip: true


Thanks in advance.

d_stranz
3rd October 2016, 18:11
Not working xml: //The Dash and xml header are added by itself.

What do you mean, "added by itself"? If you are writing the file, then it is your code that is putting in the "-" characters. If it is not your code, then the code you are using is writing invalid XML to the file.

You will not be able to bind incorrect XML to the XML model. Fix the code that writes the file so it writes complete and correct XML and you will be able to load it into the model.

Be aware that unless you add more code (a QFileSystemWatcher) to monitor any changes to the file, the model will not automatically reload itself if the file is changed from another program.