XmlListModel empty data
Solved
QML and Qt Quick
-
Hello,
I am trying to parse a local XML file using XmlListModel.
The problem is, even if the status of my model doesn't return an error code, its count property always returns 0, and my ListView stays empty.
I can't understand why it is not working, so I am asking for your help!Here is my code !
Item { property int modelStatus:listViewModel.status id: main width: 480 height: 762 state:Screen.primaryOrientation === Qt.PortraitOrientation ? "" : "Landscape" Component.onCompleted: { console.log("xml="+listViewModel.xml); console.log("status="+listViewModel.status); console.log("count="+listViewModel.count); console.log("roles="+listViewModel.roles); } onModelStatusChanged: { console.log("model status is "+modelStatus); console.log("count="+listViewModel.count); console.log("roles="+listViewModel.roles); } XmlListModel { id:listViewModel source:"gamescollection.xml" query:"/games/game" XmlRole{name:"name";query:"name/string()"} XmlRole{name:"code";query:"code/string()"} XmlRole{name:"playersmin";query:"playersmin/number()"} XmlRole{name:"playersmax";query:"playersmax/number()"} XmlRole{name:"time";query:"time/number()"} } Component { id:listViewDelegate Item{ height:64 width:gamesListView.width Text { id: rowLabel text: name+code+playersmin+playersmax+time verticalAlignment: Text.AlignVCenter font.pixelSize: 24 anchors.fill: parent } MouseArea { anchors.fill: parent onClicked: { gamesListView.currentIndex = index; } } } } ListView { id: gamesListView width: parent.width - (anchors.leftMargin*2) height: 400 contentHeight: 400 anchors.top: header1.bottom anchors.topMargin: 32 anchors.left: parent.left anchors.leftMargin: 32 model: listViewModel delegate: listViewDelegate clip:true highlight: Rectangle{ color:mainWindow.m_WidgetSelectedColor opacity: 0.25 } highlightMoveDuration: -1 highlightMoveVelocity: -1 focus:true currentIndex: 0 } }
And this is my XML file:
<?xml version=1.0" encoding="utf-8" ?> <games> <game> <name>Libertalia</name> <code>LIB</code> <playersmin>2</playersmin> <playersmax>6</playersmax> <time>60</time> </game> <game> <name>Aventuriers du Rail</name> <code>ADR</code> <playersmin>2</playersmin> <playersmax>5</playersmax> <time>90</time> </game> </games>
Thanks for your feebacks!
Baptiste
-
@bguivarch
are you sure the application finds the XML file?
You simply set the filename. Means it searches from the current working directory.Try a absolute or qrc path. In case of a absolute path precede "file:///" prefix to the url.