QFileSystemModel露
The
QFileSystemModelclass provides a data model for the local filesystem. More鈥

Synopsis露
Functions露
def
fileIcon(index)def
fileInfo(index)def
fileName(index)def
filePath(index)def
filter()def
iconProvider()def
index(path[, column=0])def
isDir(index)def
isReadOnly()def
lastModified(index)def
mkdir(parent, name)def
myComputer([role=Qt.DisplayRole])def
nameFilterDisables()def
nameFilters()def
options()def
remove(index)def
resolveSymlinks()def
rmdir(index)def
rootDirectory()def
rootPath()def
setFilter(filters)def
setIconProvider(provider)def
setNameFilterDisables(enable)def
setNameFilters(filters)def
setOption(option[, on=true])def
setOptions(options)def
setReadOnly(enable)def
setResolveSymlinks(enable)def
setRootPath(path)def
size(index)def
testOption(option)def
type(index)
Signals露
def
directoryLoaded(path)def
fileRenamed(path, oldName, newName)def
rootPathChanged(newPath)
Detailed Description露
This class provides access to the local filesystem, providing functions for renaming and removing files and directories, and for creating new directories. In the simplest case, it can be used with a suitable display widget as part of a browser or filter.
QFileSystemModelcan be accessed using the standard interface provided byQAbstractItemModel, but it also provides some convenience functions that are specific to a directory model. ThefileInfo(),isDir(),fileName()andfilePath()functions provide information about the underlying files and directories related to items in the model. Directories can be created and removed usingmkdir(),rmdir().Note
QFileSystemModelrequires an instance ofQApplication.
Example Usage露
A directory model that displays the contents of a default directory is usually constructed with a parent object:
model = QFileSystemModel() model.setRootPath(QDir.currentPath())A tree view can be used to display the contents of the model
tree = QTreeView() tree.setModel(model)and the contents of a particular directory can be displayed by setting the tree view鈥檚 root index:
tree.setRootIndex(model.index(QDir.currentPath()))The view鈥檚 root index can be used to control how much of a hierarchical model is displayed.
QFileSystemModelprovides a convenience function that returns a suitable model index for a path to a directory within the model.
Caching and Performance露
QFileSystemModelwill not fetch any files or directories untilsetRootPath()is called. This will prevent any unnecessary querying on the file system until that point such as listing the drives on Windows.Unlike
QDirModel,QFileSystemModeluses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls torowCount()will return 0 until the model populates a directory.
QFileSystemModelkeeps a cache with file information. The cache is automatically kept up to date using theQFileSystemWatcher.See also
- class PySide2.QtWidgets.QFileSystemModel([parent=None])露
- param parent:
Constructs a file system model with the given
parent.
- PySide2.QtWidgets.QFileSystemModel.Roles露
Constant
Description
QFileSystemModel.FileIconRole
QFileSystemModel.FilePathRole
QFileSystemModel.FileNameRole
QFileSystemModel.FilePermissions
- PySide2.QtWidgets.QFileSystemModel.Option露
Constant
Description
QFileSystemModel.DontWatchForChanges
Do not add file watchers to the paths. This reduces overhead when using the model for simple tasks like line edit completion.
QFileSystemModel.DontResolveSymlinks
Don鈥檛 resolve symlinks in the file system model. By default, symlinks are resolved.
QFileSystemModel.DontUseCustomDirectoryIcons
Always use the default directory icon. Some platforms allow the user to set a different icon. Custom icon lookup causes a big performance impact over network or removable drives. This sets the
DontUseCustomDirectoryIconsoption in the icon provider accordingly.See also
New in version 5.14.
- PySide2.QtWidgets.QFileSystemModel.directoryLoaded(path)露
- Parameters:
path 鈥 str
- PySide2.QtWidgets.QFileSystemModel.fileIcon(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
Returns the icon for the item stored in the model under the given
index.
- PySide2.QtWidgets.QFileSystemModel.fileInfo(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
Returns the
QFileInfofor the item stored in the model under the givenindex.
- PySide2.QtWidgets.QFileSystemModel.fileName(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
str
Returns the file name for the item stored in the model under the given
index.
- PySide2.QtWidgets.QFileSystemModel.filePath(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
str
Returns the path of the item stored in the model under the
indexgiven.
- PySide2.QtWidgets.QFileSystemModel.fileRenamed(path, oldName, newName)露
- Parameters:
path 鈥 str
oldName 鈥 str
newName 鈥 str
- PySide2.QtWidgets.QFileSystemModel.filter()露
- Return type:
Filters
Returns the filter specified for the directory model.
If a filter has not been set, the default filter is
AllEntries|NoDotAndDotDot|AllDirs.See also
setFilter()Filters
- PySide2.QtWidgets.QFileSystemModel.iconProvider()露
- Return type:
Returns the file icon provider for this directory model.
See also
- PySide2.QtWidgets.QFileSystemModel.index(path[, column=0])露
- Parameters:
path 鈥 str
column 鈥 int
- Return type:
This is an overloaded function.
Returns the model item index for the given
pathandcolumn.
- PySide2.QtWidgets.QFileSystemModel.isDir(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
bool
Returns
trueif the model itemindexrepresents a directory; otherwise returnsfalse.
- PySide2.QtWidgets.QFileSystemModel.isReadOnly()露
- Return type:
bool
This property holds Whether the directory model allows writing to the file system.
If this property is set to false, the directory model will allow renaming, copying and deleting of files and directories.
This property is
trueby default
- PySide2.QtWidgets.QFileSystemModel.lastModified(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
Returns the date and time when
indexwas last modified.
- PySide2.QtWidgets.QFileSystemModel.mkdir(parent, name)露
- Parameters:
parent 鈥
PySide2.QtCore.QModelIndexname 鈥 str
- Return type:
Create a directory with the
namein theparentmodel index.
- PySide2.QtWidgets.QFileSystemModel.myComputer([role=Qt.DisplayRole])露
- Parameters:
role 鈥 int
- Return type:
object
Returns the data stored under the given
rolefor the item 鈥淢y Computer鈥.See also
ItemDataRole
- PySide2.QtWidgets.QFileSystemModel.nameFilterDisables()露
- Return type:
bool
This property holds Whether files that don鈥檛 pass the name filter are hidden or disabled.
This property is
trueby default
- PySide2.QtWidgets.QFileSystemModel.nameFilters()露
- Return type:
list of strings
Returns a list of filters applied to the names in the model.
See also
- PySide2.QtWidgets.QFileSystemModel.options()露
- Return type:
Options
See also
- PySide2.QtWidgets.QFileSystemModel.remove(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
bool
Removes the model item
indexfrom the file system model and deletes the corresponding file from the file system , returning true if successful. If the item cannot be removed, false is returned.Warning
This function deletes files from the file system; it does not move them to a location where they can be recovered.
See also
- PySide2.QtWidgets.QFileSystemModel.resolveSymlinks()露
- Return type:
bool
This property holds Whether the directory model should resolve symbolic links.
This is only relevant on Windows.
By default, this property is
true.See also
Options
- PySide2.QtWidgets.QFileSystemModel.rmdir(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
bool
Removes the directory corresponding to the model item
indexin the file system model and deletes the corresponding directory from the file system , returning true if successful. If the directory cannot be removed, false is returned.Warning
This function deletes directories from the file system; it does not move them to a location where they can be recovered.
See also
- PySide2.QtWidgets.QFileSystemModel.rootDirectory()露
- Return type:
The currently set directory
See also
- PySide2.QtWidgets.QFileSystemModel.rootPath()露
- Return type:
str
The currently set root path
See also
- PySide2.QtWidgets.QFileSystemModel.rootPathChanged(newPath)露
- Parameters:
newPath 鈥 str
- PySide2.QtWidgets.QFileSystemModel.setFilter(filters)露
- Parameters:
filters 鈥
Filters
Sets the directory model鈥檚 filter to that specified by
filters.Note that the filter you set should always include the
AllDirsenum value, otherwiseQFileSystemModelwon鈥檛 be able to read the directory structure.See also
filter()Filters
- PySide2.QtWidgets.QFileSystemModel.setIconProvider(provider)露
- Parameters:
provider 鈥
PySide2.QtWidgets.QFileIconProvider
Sets the
providerof file icons for the directory model.See also
- PySide2.QtWidgets.QFileSystemModel.setNameFilterDisables(enable)露
- Parameters:
enable 鈥 bool
This property holds Whether files that don鈥檛 pass the name filter are hidden or disabled.
This property is
trueby default
- PySide2.QtWidgets.QFileSystemModel.setNameFilters(filters)露
- Parameters:
filters 鈥 list of strings
Sets the name
filtersto apply against the existing files.See also
- PySide2.QtWidgets.QFileSystemModel.setOption(option[, on=true])露
- Parameters:
option 鈥
Optionon 鈥 bool
Sets the given
optionto be enabled ifonis true; otherwise, clears the givenoption.Options should be set before changing properties.
See also
options
testOption()
- PySide2.QtWidgets.QFileSystemModel.setOptions(options)露
- Parameters:
options 鈥
Options
See also
- PySide2.QtWidgets.QFileSystemModel.setReadOnly(enable)露
- Parameters:
enable 鈥 bool
This property holds Whether the directory model allows writing to the file system.
If this property is set to false, the directory model will allow renaming, copying and deleting of files and directories.
This property is
trueby default
- PySide2.QtWidgets.QFileSystemModel.setResolveSymlinks(enable)露
- Parameters:
enable 鈥 bool
This property holds Whether the directory model should resolve symbolic links.
This is only relevant on Windows.
By default, this property is
true.See also
Options
- PySide2.QtWidgets.QFileSystemModel.setRootPath(path)露
- Parameters:
path 鈥 str
- Return type:
Sets the directory that is being watched by the model to
newPathby installing afile system watcheron it. Any changes to files and directories within this directory will be reflected in the model.If the path is changed, the
rootPathChanged()signal will be emitted.Note
This function does not change the structure of the model or modify the data available to views. In other words, the 鈥渞oot鈥 of the model is not changed to include only files and directories within the directory specified by
newPathin the file system.See also
- PySide2.QtWidgets.QFileSystemModel.size(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
int
Returns the size in bytes of
index. If the file does not exist, 0 is returned.
- PySide2.QtWidgets.QFileSystemModel.testOption(option)露
- Parameters:
option 鈥
Option- Return type:
bool
Returns
trueif the givenoptionis enabled; otherwise, returns false.See also
options
setOption()
- PySide2.QtWidgets.QFileSystemModel.type(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
str
Returns the type of file
indexsuch as 鈥淒irectory鈥 or 鈥淛PEG file鈥.
漏 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.