如何在Qml中列出文件夹内容?

Ale*_*oux 3 application-development qml ubuntu-sdk

我正在尝试重写笔记应用程序的一部分,以便将笔记直接存储在文件系统中。但我希望能够列出文件夹中的文件,不是为了显示它们,而是为了在 javascript 函数中处理它们。

and*_*ing 5

以下是如何使用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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

看起来像:

文件夹视图

现在当然,您想用这些信息做一些事情。不幸的是,您不能像Beru 开发人员博客上的这篇文章中解释的那样简单地迭代模型。他很有帮助地展示了如何使用该Repeater组件:

    Repeater {
        model: folderModel

        Component {
            Item {
                Component.onCompleted: {
                    // Do something interesting here...
                    console.log(fileName)
                }
            }
        }
    } 
Run Code Online (Sandbox Code Playgroud)