FolderListModel QML Type

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

import 语句: import Qt.labs.folderlistmodel 2.5

特性

方法

详细描述

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
					

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
  • fileURL (since Qt 5.2; deprecated since Qt 5.15)
  • fileUrl (since Qt 5.15)
  • fileBaseName
  • fileSuffix
  • fileSize
  • fileModified
  • fileAccessed
  • fileIsDir

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, navigation directories can also be excluded by setting the showDotAndDotDot property to false, hidden files can be included or excluded using the showHidden 特性。

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
import Qt.labs.folderlistmodel
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 .

特性文档编制

caseSensitive : bool

Use case sensitive pattern matching.

默认情况下,此特性为 true。


count : int

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


folder : url

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

The value must be a file : or qrc : URL, or a relative URL.

The default 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 the current folder .


rootFolder : url

When this property is set, the given folder will be treated as the root in the file system, so that you can only traverse subfolders within it.


showDirs : bool

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

默认情况下,此特性为 true。

注意, nameFilters are not applied to directories.

另请参阅 showDotAndDotDot .


showDirsFirst : bool

If true, if directories are included in the model they will always be shown first, then the files.

默认情况下,此特性为 false。


showDotAndDotDot : bool

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

默认情况下,此特性为 false。

另请参阅 showDirs .


showFiles : bool

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

默认情况下,此特性为 true。

另请参阅 showDirs .


showHidden : bool

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

默认情况下,此特性为 false。


showOnlyReadable : bool

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

默认情况下,此特性为 false。

另请参阅 showDirs .


sortCaseSensitive : bool

若设为 true , the sort is case sensitive. This property is true 在默认情况下。


sortField : enumeration

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

常量 描述
Unsorted no sorting is applied
名称 sort by filename
时间 sort by time modified
Size sort by file size
类型 sort by file type (extension)

另请参阅 sortReversed .


sortReversed : bool

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

另请参阅 sortField .


status : enumeration

This property holds the status of folder reading. It can be one of:

常量 描述
FolderListModel.Null no folder has been set
FolderListModel.Ready the folder has been loaded
FolderListModel.Loading the folder is currently being loaded

Use this status to provide an update or respond to the status change in some way. For example, you could:

  • Trigger a state change:
    State { name: 'loaded'; when: folderModel.status == FolderListModel.Ready }
    																							
  • Implement an onStatusChanged signal handler:
    FolderListModel {
    
    id
    
    :
    
    folderModel
    
    
    onStatusChanged
    
    :
    
    if
    
     (
    
    folderModel
    
    .
    
    status
    
    
    ==
    
    
    FolderListModel
    
    .
    
    Ready
    
    )
    
    console
    
    .
    
    log
    
    (
    
    'Loaded'
    
    )
    }
    																							
  • Bind to the status value:
    Text { text: folderModel.status == FolderListModel.Ready ? 'Loaded' : 'Not loaded' }
    																							

方法文档编制

var get ( int index , string property )

Returns the folder property 为给定 index . The following properties are available:

  • fileName
  • filePath
  • fileURL (since Qt 5.2; deprecated since Qt 5.15)
  • fileUrl (since Qt 5.15)
  • fileBaseName
  • fileSuffix
  • fileSize
  • fileModified
  • fileAccessed
  • fileIsDir

int indexOf ( url file )

返回索引为给定 file URL if the model contains it, or -1 if not.


bool isFolder ( int index )

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