3

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?

3
  • 1
    You 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.
    – mlvljr
    Commented 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
    – SoH
    Commented 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?
    – mlvljr
    Commented Jul 6, 2012 at 12:03

2 Answers 2

2

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.

3
  • 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.
    – SoH
    Commented 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.
    – MartinJ
    Commented Jul 19, 2012 at 10:08
  • Forced to use this approach to recreate whole model as FolderListModel does not have ...Changed signals/events for many properties like: nameFilters, showDotAndDotDot and others which is VERY ugly!!! Why Qt did not add them?! Commented May 12, 2020 at 23:21
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.