How to list folder contents in Qml?

5,140

Here's a quick example of how to display the contents of a folder using a FolderListModel:

import QtQuick 2.0
import Qt.labs.folderlistmodel 1.0
import Ubuntu.Components 0.1
import Ubuntu.Components.ListItems 0.1 as ListItem

MainView {
    id: root
    width: units.gu(50)
    height: units.gu(75)

    Page {
        id: home
        visible: true
        title: "Files"

        FolderListModel {
            id: folderModel
            folder: "/"
            nameFilters: [ "*" ]
        }

        ListView {
            anchors.fill: parent
            model: folderModel
            delegate: ListItem.Standard {
                text: model.fileName
            }
        }
    }
}

It looks like:

folder view

Now of course, you want to do something with this information. Unfortunately, you can't iterate over the model as simply as you'd like to as explained in this post on the Beru developer's blog. He helpfully shows how to use the Repeater component:

    Repeater {
        model: folderModel

        Component {
            Item {
                Component.onCompleted: {
                    // Do something interesting here...
                    console.log(fileName)
                }
            }
        }
    } 
Share:
5,140

Related videos on Youtube

Alexandre Roux
Author by

Alexandre Roux

Updated on September 18, 2022

Comments

  • Alexandre Roux
    Alexandre Roux over 1 year

    I'm trying to rewrite a part of the notes-app in order to store notes directly in the file system. But I'd like to be able to list files in a folder, not in order to display them, but to process them in a javascript function.