Important: Please read the Qt Code of Conduct -

How to make QFileSystemModel work with a ListView

  • Hi, I am unable to make the QFileSystemModel go along with the ListView. All the examples and documentation that I found would use QFileSystemModel with the TreeView (which works fine), but this is not what I need.

    I do not want to use FolderListModel QML type either because I want to add extra roles to my model later and edit/delete files and folders from the application.

    I used DelegateModel to be able to set an other root index. For some reason, my files are correctly displayed for a fraction of a second and then the view jumps to the root of my filesystem. I'm not sure what happens, maybe the indexes are invalidated somehow.

    Below is a sample of my code:


        QFileSystemModel *fsm = new QFileSystemModel(&engine);
        engine.rootContext()->setContextProperty("displayFileSystemModel", fsm);
        engine.rootContext()->setContextProperty("rootPathIndex", fsm->index(fsm->rootPath()));


    ListView {
        width: 300
        height: 400
        model: DelegateModel {
            model: displayFileSystemModel
            rootIndex: rootPathIndex
            delegate: Rectangle {
                width: 200; height: 25
                Text {
                    text: model.filePath
                    Component.onCompleted: {

    What am I doing wrong?


    So, apparently QFileSystemModel uses a separate thread to populate itself and shoots a directoryLoaded signal once the the thread has finished to load the path.

    I tried connecting my view to only set the root index once the path has been properly loaded by adding the following code to my listview

        Connections {
            target: displayFileSystemModel
            function onDirectoryLoaded(path) { delegatemodel.rootIndex = rootPathIndex }

    However, this does not solve the problem. A dirty workaround was to set a timer to try set the rootIndex after a period of time.

        Timer {
            interval: 100
            running: true
            onTriggered: delegatemodel.rootIndex = examPathIndex

    And this "solves" the problem but of course is far from a satisfactory solution. Any idea?

    UPDATE 2

    Turns out every time the model is updated (and the directoryLoaded signal is triggered), the view resets. Even trying to reassign the rootIndex every time new data is loaded doesn't work.

  • I think the context property "rootPathIndex" is doing the equivalent of storing an index (QModelIndex). You should not store indexes they can go bad. Instead create a getter or property on your QFileSystemModel (create subclass) to get the root path index on the fly (recreated) each time your delegate is reloaded.

Log in to reply