QFileSystemModel#
The QFileSystemModel
class provides a data model for the local filesystem. More…
Synopsis#
Properties#
nameFilterDisables
- Whether files that don’t pass the name filter are hidden or disabledoptions
- The various options that affect the modelreadOnly
- Whether the directory model allows writing to the file systemresolveSymlinks
- Whether the directory model should resolve symbolic links
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
lastModified
(index, tz)def
mkdir
(parent, name)def
myComputer
([role=Qt.DisplayRole])def
nameFilterDisables
()def
nameFilters
()def
options
()def
permissions
(index)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)
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
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.
QFileSystemModel
can be accessed using the standard interface provided by QAbstractItemModel, but it also provides some convenience functions that are specific to a directory model. The fileInfo()
, isDir()
, fileName()
and filePath()
functions provide information about the underlying files and directories related to items in the model. Directories can be created and removed using mkdir()
, rmdir()
.
Note
QFileSystemModel
requires an instance of QApplication.
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()) if __name__ == "__main__": app = QApplication([]) splitter = QSplitter()
A tree view can be used to display the contents of the model
tree = QTreeView(splitter) tree.setModel(model)
and the contents of a particular directory can be displayed by setting the tree view’s root index:
tree.setRootIndex(model.index(QDir.currentPath()))
The view’s root index can be used to control how much of a hierarchical model is displayed. QFileSystemModel
provides a convenience function that returns a suitable model index for a path to a directory within the model.
Caching and Performance#
QFileSystemModel
will not fetch any files or directories until setRootPath()
is called. This will prevent any unnecessary querying on the file system until that point such as listing the drives on Windows.
QFileSystemModel
uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls to rowCount()
will return 0 until the model populates a directory.
QFileSystemModel
keeps a cache with file information. The cache is automatically kept up to date using the QFileSystemWatcher.
See also
Model Classes
- class PySide6.QtWidgets.QFileSystemModel([parent=None])#
- Parameters:
parent –
PySide6.QtCore.QObject
Constructs a file system model with the given parent
.
Note
Properties can be used directly when from __feature__ import true_property
is used or via accessor functions otherwise.
- property PᅟySide6.QtWidgets.QFileSystemModel.nameFilterDisables: bool#
This property holds Whether files that don’t pass the name filter are hidden or disabled.
This property is true
by default
- Access functions:
setNameFilterDisables
(enable)
- property PᅟySide6.QtWidgets.QFileSystemModel.options: Combination of QAbstractFileIconProvider.Option#
This property holds the various options that affect the model.
By default, all options are disabled.
Options should be set before changing properties.
See also
- Access functions:
options
()setOptions
(options)
- property PᅟySide6.QtWidgets.QFileSystemModel.readOnly: 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 true
by default
- Access functions:
isReadOnly
()setReadOnly
(enable)
- property PᅟySide6.QtWidgets.QFileSystemModel.resolveSymlinks: 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
- Access functions:
setResolveSymlinks
(enable)
- PySide6.QtWidgets.QFileSystemModel.Roles#
Constant
Description
QFileSystemModel.FileIconRole
QFileSystemModel.FilePathRole
QFileSystemModel.FileNameRole
QFileSystemModel.FilePermissions
- PySide6.QtWidgets.QFileSystemModel.Option#
Constant
Description
QFileSystemModel.DontWatchForChanges
(inherits
enum.Flag
) 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’t 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 QFileIconProvider::DontUseCustomDirectoryIcons option in the icon provider accordingly.
See also
- PySide6.QtWidgets.QFileSystemModel.directoryLoaded(path)#
- Parameters:
path – str
This signal is emitted when the gatherer thread has finished to load the path
.
- PySide6.QtWidgets.QFileSystemModel.fileIcon(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
Returns the icon for the item stored in the model under the given index
.
- PySide6.QtWidgets.QFileSystemModel.fileInfo(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
Returns the QFileInfo for the item stored in the model under the given index
.
- PySide6.QtWidgets.QFileSystemModel.fileName(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
str
Returns the file name for the item stored in the model under the given index
.
- PySide6.QtWidgets.QFileSystemModel.filePath(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
str
Returns the path of the item stored in the model under the index
given.
- PySide6.QtWidgets.QFileSystemModel.fileRenamed(path, oldName, newName)#
- Parameters:
path – str
oldName – str
newName – str
This signal is emitted whenever a file with the oldName
is successfully renamed to newName
. The file is located in the directory path
.
- PySide6.QtWidgets.QFileSystemModel.filter()#
- Return type:
Combination of
QDir.Filter
Returns the filter specified for the directory model.
If a filter has not been set, the default filter is QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs.
See also
setFilter()
Filters
- PySide6.QtWidgets.QFileSystemModel.iconProvider()#
- Return type:
Returns the file icon provider for this directory model.
See also
- PySide6.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 path
and column
.
- PySide6.QtWidgets.QFileSystemModel.isDir(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
bool
Returns true
if the model item index
represents a directory; otherwise returns false
.
- PySide6.QtWidgets.QFileSystemModel.isReadOnly()#
- Return type:
bool
Getter of property readOnly
.
- PySide6.QtWidgets.QFileSystemModel.lastModified(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
Returns the date and time (in local time) when index
was last modified.
This is an overloaded function, equivalent to calling:
lastModified(index, QTimeZone::LocalTime);
If index
is invalid, a default constructed QDateTime is returned.
- PySide6.QtWidgets.QFileSystemModel.lastModified(index, tz)
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
Returns the date and time, in the time zone tz
, when index
was last modified.
Typical arguments for tz
are QTimeZone::UTC
or QTimeZone::LocalTime
. UTC does not require any conversion from the time returned by the native file system API, therefore getting the time in UTC is potentially faster. LocalTime is typically chosen if the time is shown to the user.
If index
is invalid, a default constructed QDateTime is returned.
- PySide6.QtWidgets.QFileSystemModel.mkdir(parent, name)#
- Parameters:
parent –
PySide6.QtCore.QModelIndex
name – str
- Return type:
Create a directory with the name
in the parent
model index.
- PySide6.QtWidgets.QFileSystemModel.myComputer([role=Qt.DisplayRole])#
- Parameters:
role – int
- Return type:
object
Returns the data stored under the given role
for the item “My Computer”.
See also
ItemDataRole
- PySide6.QtWidgets.QFileSystemModel.nameFilterDisables()#
- Return type:
bool
See also
Getter of property nameFilterDisables
.
- PySide6.QtWidgets.QFileSystemModel.nameFilters()#
- Return type:
list of strings
Returns a list of filters applied to the names in the model.
See also
- PySide6.QtWidgets.QFileSystemModel.options()#
- Return type:
Combination of
QFileSystemModel.Option
See also
- PySide6.QtWidgets.QFileSystemModel.permissions(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
Combination of
QFileDevice.Permission
Returns the complete OR-ed together combination of QFile::Permission for the index
.
- PySide6.QtWidgets.QFileSystemModel.remove(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
bool
Removes the model item index
from 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
- PySide6.QtWidgets.QFileSystemModel.resolveSymlinks()#
- Return type:
bool
See also
Getter of property resolveSymlinks
.
- PySide6.QtWidgets.QFileSystemModel.rmdir(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
bool
Removes the directory corresponding to the model item index
in 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
- PySide6.QtWidgets.QFileSystemModel.rootDirectory()#
- Return type:
The currently set directory
See also
- PySide6.QtWidgets.QFileSystemModel.rootPath()#
- Return type:
str
The currently set root path
See also
- PySide6.QtWidgets.QFileSystemModel.rootPathChanged(newPath)#
- Parameters:
newPath – str
This signal is emitted whenever the root path has been changed to a newPath
.
- PySide6.QtWidgets.QFileSystemModel.setFilter(filters)#
- Parameters:
filters – Combination of
QDir.Filter
Sets the directory model’s filter to that specified by filters
.
Note that the filter you set should always include the QDir::AllDirs enum value, otherwise QFileSystemModel
won’t be able to read the directory structure.
See also
filter()
Filters
- PySide6.QtWidgets.QFileSystemModel.setIconProvider(provider)#
- Parameters:
provider –
PySide6.QtGui.QAbstractFileIconProvider
Sets the provider
of file icons for the directory model.
See also
- PySide6.QtWidgets.QFileSystemModel.setNameFilterDisables(enable)#
- Parameters:
enable – bool
See also
Setter of property nameFilterDisables
.
- PySide6.QtWidgets.QFileSystemModel.setNameFilters(filters)#
- Parameters:
filters – list of strings
Sets the name filters
to apply against the existing files.
See also
- PySide6.QtWidgets.QFileSystemModel.setOption(option[, on=true])#
- Parameters:
option –
Option
on – bool
Sets the given option
to be enabled if on
is true; otherwise, clears the given option
.
Options should be set before changing properties.
See also
- PySide6.QtWidgets.QFileSystemModel.setOptions(options)#
- Parameters:
options – Combination of
QFileSystemModel.Option
See also
- PySide6.QtWidgets.QFileSystemModel.setReadOnly(enable)#
- Parameters:
enable – bool
See also
Setter of property readOnly
.
- PySide6.QtWidgets.QFileSystemModel.setResolveSymlinks(enable)#
- Parameters:
enable – bool
See also
Setter of property resolveSymlinks
.
- PySide6.QtWidgets.QFileSystemModel.setRootPath(path)#
- Parameters:
path – str
- Return type:
Sets the directory that is being watched by the model to newPath
by installing a file system watcher on 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 “root” of the model is not changed to include only files and directories within the directory specified by newPath
in the file system.
See also
- PySide6.QtWidgets.QFileSystemModel.size(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
int
Returns the size in bytes of index
. If the file does not exist, 0 is returned.
- PySide6.QtWidgets.QFileSystemModel.testOption(option)#
- Parameters:
option –
Option
- Return type:
bool
Returns true
if the given option
is enabled; otherwise, returns false.
See also
- PySide6.QtWidgets.QFileSystemModel.type(index)#
- Parameters:
index –
PySide6.QtCore.QModelIndex
- Return type:
str
Returns the type of file index
such as “Directory” or “JPEG file”.