Up to now, Beru has just listed whatever files it can find in ~/Books
. But it’d be better to store information about these files in a database, where we can also store titles, authors, and cover images. But we still want to automatically includes the files in ~/Books
, so we need a way of going through all those files.
If you remember the previous episode, you’ll recall that we used a FolderListModel
to access the list of files. In QML, a ListModel
is the thing that holds the data that’s going to be displayed in a list. Usually, you can go through its elements programatically:
for (var i=0; i<model.count; i++) {
var item = model.get(i)
// Do something
}
But this doesn’t work with a FolderListModel
—it doesn’t have a count
attribute. Luckily, the music app people have a solution: Make a Repeater
using this model, create an Item
for each entry, and run code in its Component.onCompleted
. Here’s the basic idea.
FolderListModel {
id: folderModel
// various settings
}
Repeater {
model: folderModel
Component {
Item {
Component.onCompleted: // Do something
}
}
}
It’s clever, but I do wonder why (or if) it’s necessary.