ListView model update
-
hello
i have ListView that displays list of files in folder.ListView{ model: fileChecker.getFilesCount(folderID); delegate: Item{ Button{ anchors.fill: parent text: fileChecker.getFileName(index) } } }
fileChecker class checks files in folder and returns their names.
when window creates, it have list of files, everething is ok. but i need to update list if files appears or disappears in the folder. how can i update ListView?
-
List view have property called model. Have you seen the ListModel methods ? like append, insert, delete ?
-
Hi @gogoer ,
since fileChecker is a (c++?) class and you fetch your model via a function call, the model is only loaded once, during creation and no further updated.You have 2 options.
- make it a proper Q_Property than your list view will update automatically
- create a signal in your class that is emitted once the model changes and manually react to that signal in qml to reset the model
//for example Connections{ target: fileChecker onFilesCountChanged: myListView.model = fileChecker.getFilesCount(folderID) }
-
Hi @gogoer
Can you try using QAbstractListModel Class from QAbstractItemModel and expose it to QML.
Where you can pass the class toListview { model }
and you get all the options from the C++ Class.
ExampleOr
As per my understanding in your present code you have to expose the update signal to QML so the model can be updated based on the signal. -
@gogoer said in ListView model update:
updateModel
are you sure
updateModel
is called ?
are you surefileChecker.getFilesCount
actually returns a list with the new entries?function updateModel(){ console.log("Attempt to update model") var newModel = fileChecker.getFilesCount(folderID); console.log("new Model", newModel) myListView.model = newModel }