XMLListModel inside a repeater (nested models)
Solved
QML and Qt Quick
-
Hi,
I'm trying to generate nested ListViews based on multiple XMLListModel queries (different queries on the same source file).
I can succesfully create the outer list, using the outerModel "count", but the innerList only displays the first element of the inner model and not all of them (despite the model count being correct)What am I doing wrong? May the problem be related to some sort of race condition and/or model loading?
My minimum test code is below.
Thank you for your helpStef
main.qml
import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Window 2.15 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") LabBook { anchors.fill: parent } }
LabBook.qml
import QtQuick 2.0 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.14 import QtQuick.XmlListModel 2.0 Item { XmlListModel { id: outerModel source: "qrc:/labbook.xml" query: "/book/plate" XmlRole { name: "id_plate"; query: "@id/number()" } } ColumnLayout { anchors.fill: parent anchors.margins: 10 // content RowLayout { // plates ListView { id: listPlates Layout.preferredWidth: 200 Layout.fillHeight: true model: outerModel delegate: Button { text: "Plate " + id_plate } } // notes ColumnLayout { Repeater { id: rep model: outerModel.count delegate: Rectangle { Layout.preferredHeight: 100 Layout.fillWidth: true color: "green" XmlListModel { id: innerModel source: outerModel.source query: "/book/plate[" + (modelData+1) + "]/session" XmlRole { name: "day"; query: "@day/string()" } } // title Label { id: title text: "Plate " + outerModel.get(modelData).id_plate } // list of notes ListView { anchors.top: title.bottom id: listNotes model: innerModel delegate: Text { height: 25; text: day } onCountChanged: console.log("ListView count: ", count) } } } } } } }
labbook.xml
<?xml version="1.0" encoding="utf-8"?> <book> <plate id="123"> <session day="A1"></session> <session day="A2"></session> <session day="A3"></session> </plate> <plate id="456"> <session day="B1"></session> <session day="B2"></session> </plate> <plate id="789"> <session day="C1" dd="01" mm="03" yyyy="2021" /> <session day="C2" dd="01" mm="03" yyyy="2021" /> </plate> </book>
-
The problem is solved making the height of the "listNotes" explicit, like
ListView { anchors.top: title.bottom id: listNotes height: 50 model: innerModel delegate: Text { height: 25; text: day } onCountChanged: console.log("ListView count: ", count) }
or similarly with anchors.fill, Layout, etc...