FolderListModel QML Element

The FolderListModel provides a model of the contents of a file system folder. 更多...

特性

方法

詳細描述

FolderListModel provides access to information about the contents of a folder in the local file system, exposing a list of files to views and other data components.

注意: This type is made available by importing the Qt.labs.folderlistmodel 模塊。 Elements in the Qt.labs module are not guaranteed to remain compatible in future versions.

import Qt.labs.folderlistmodel 1.0

The folder property specifies the folder to access. Information about the files and directories in the folder is supplied via the model's interface. Components access names and paths via the following roles:

  • fileName
  • filePath

Additionally a file entry can be differentiated from a folder entry via the isFolder() 方法。

過濾

Various properties can be set to filter the number of files and directories exposed by the model.

The nameFilters property can be set to contain a list of wildcard filters that are applied to names of files and directories, causing only those that match the filters to be exposed.

Directories can be included or excluded using the showDirs property, and navigation directories can also be excluded by setting the showDotAndDotDot property to false.

It is sometimes useful to limit the files and directories exposed to those that the user can access. The showOnlyReadable property can be set to enable this feature.

用法範例

The following example shows a FolderListModel being used to provide a list of QML files in a ListView :

import QtQuick 1.0
import Qt.labs.folderlistmodel 1.0
ListView {
    width: 200; height: 400
    FolderListModel {
        id: folderModel
        nameFilters: ["*.qml"]
    }
    Component {
        id: fileDelegate
        Text { text: fileName }
    }
    model: folderModel
    delegate: fileDelegate
}
					
					

Path Separators

Qt uses "/" as a universal directory separator in the same way that "/" is used as a path separator in URLs. If you always use "/" as a directory separator, Qt will translate your paths to conform to the underlying operating system.

另請參閱 QML Data Models .

特性文檔編製

count : int

Returns the number of items in the current folder that match the filter criteria.

folder : string

The folder property holds a URL for the folder that the model is currently providing.

The value is a URL expressed as a string, and must be a file : or qrc : URL, or a relative URL.

By default, the value is an invalid URL.

nameFilters : list < string >

The nameFilters property contains a list of file name filters. The filters may include the ? and * wildcards.

The example below filters on PNG and JPEG files:

FolderListModel {
    nameFilters: [ "*.png", "*.jpg" ]
}
								

注意: Directories are not excluded by filters.

parentFolder : url

Returns the URL of the parent of of the current folder .

showDirs : bool

If true, directories are included in the model; otherwise only files are included.

默認情況下,此特性為 true。

注意, nameFilters are not applied to directories.

另請參閱 showDotAndDotDot .

showDotAndDotDot : bool

If true, the "." and ".." directories are included in the model; otherwise they are excluded.

默認情況下,此特性為 false。

另請參閱 showDirs .

showOnlyReadable : bool

If true, only readable files and directories are shown; otherwise all files and directories are shown.

默認情況下,此特性為 false。

另請參閱 showDirs .

sortField : enumeration

The sortField property contains field to use for sorting. sortField may be one of:

  • Unsorted - no sorting is applied. The order is system default.
  • Name - sort by filename
  • Time - sort by time modified
  • Size - sort by file size
  • Type - sort by file type (extension)

另請參閱 sortReversed .

sortReversed : bool

If set to true, reverses the sort order. The default is false.

另請參閱 sortField .


方法文檔編製

bool isFolder ( int index )

Returns true if the entry index is a folder; otherwise returns false.