I have created a normal tab bar and am showing music files in a listview from a specific directory from my sd card. I want the list to be updated when I download a new file in the directory. But since i have show the list in a tab bar, the qml is loaded in the beginning. How do I imlement this?
-
1You can add a Timer updating your (QML ListModel) model, or implement a model on the C++ side togeteher with the Timer. Whatever you choose, when the model updates, the ListView will update too.– mlvljrCommented Jul 6, 2012 at 10:54
-
I am using FolderListModel. It doesnt have a clear() function...other ListModels have do. Please can u give me code to refresh my list using FolderListmodel? Thanks– SoHCommented Jul 6, 2012 at 11:01
-
Well, I don't have much time right now, but may be you could create a new model on timer events and swap it into the view?– mlvljrCommented Jul 6, 2012 at 12:03
Add a comment
|
2 Answers
If it is an option for you, I'd grab the QtQuick 2.0 implementation and use that instead. It will automatically react to changes in the folder.
Otherwise, you'll have little choice but to recreate the model, e.g.
import QtQuick 1.1
import Qt.labs.folderlistmodel 1.0
ListView {
id: view
model: folderModel
delegate: Text { text: fileName }
property variant folderModel
function updateModel() {
var folder = ""
if (folderModel) {
folder = folderModel.folder
folderModel.destroy()
}
folderModel = modelComponent.createObject(view, {"folder": folder})
}
Component {
id: modelComponent
FolderListModel {}
}
Component.onCompleted: updateModel()
}
Ugly, but it works.
-
I am using the listview in a tab bar layout. So the Component.onCompleted() function of the listview will only be called once when the listview has been completed.– SoHCommented Jul 19, 2012 at 6:30
-
The initial model is created in Component.onCompleted. You will need to call updateModel() whenever you want to refresh the view.– MartinJCommented Jul 19, 2012 at 10:08
-
Forced to use this approach to recreate whole model as
FolderListModeldoes not have...Changedsignals/events for many properties like:nameFilters,showDotAndDotDotand others which is VERY ugly!!! Why Qt did not add them?! Commented May 12, 2020 at 23:21
Looks like FolderListModel update the list automatically once new files are added or remove from path:(Qt5.7.1)
ListView
{
id:myListViewUSB
anchors.fill: parent
FolderListModel
{
id: folderModelUSB
folder: "file:///mnt/usb"
}
model: folderModelUSB
delegate:
Text
{
text: fileName
anchors.horizontalCenter: parent.horizontalCenter
}
}
I testet by adding and removing file on my folder and it appears automatically on the ListView.