QFileSystemModel Class

The QFileSystemModel class provides a data model for the local filesystem. More...

Header: #include <QFileSystemModel>
qmake: QT += widgets
Since: Qt 4.4
Inherits: QAbstractItemModel

This class was introduced in Qt 4.4.

Public Types

enum Option { DontWatchForChanges, DontResolveSymlinks, DontUseCustomDirectoryIcons }
enum Roles { FileIconRole, FilePathRole, FileNameRole, FilePermissions }

Properties

Public Functions

bool isReadOnly() const
bool nameFilterDisables() const
QFileSystemModel::Options options() const
bool resolveSymlinks() const
void setNameFilterDisables(bool enable)
void setOptions(QFileSystemModel::Options options)
void setReadOnly(bool enable)
void setResolveSymlinks(bool enable)

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.

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:

     QFileSystemModel *model = new QFileSystemModel;
     model->setRootPath(QDir::currentPath());

A tree view can be used to display the contents of the model

     QTreeView *tree = new 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.

Unlike QDirModel, 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.

Member Type Documentation

enum QFileSystemModel::Option

ConstantValueDescription
QFileSystemModel::DontWatchForChanges0x00000001Do not add file watchers to the paths. This reduces overhead when using the model for simple tasks like line edit completion.
QFileSystemModel::DontResolveSymlinks0x00000002Don't resolve symlinks in the file system model. By default, symlinks are resolved.
QFileSystemModel::DontUseCustomDirectoryIcons0x00000004Always 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.

This enum was introduced or modified in Qt 5.14.

See also resolveSymlinks.

enum QFileSystemModel::Roles

ConstantValue
QFileSystemModel::FileIconRoleQt::DecorationRole
QFileSystemModel::FilePathRoleQt::UserRole + 1
QFileSystemModel::FileNameRoleQt::UserRole + 2
QFileSystemModel::FilePermissionsQt::UserRole + 3

Property Documentation

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:

bool nameFilterDisables() const
void setNameFilterDisables(bool enable)

options : Options

This property holds the various options that affect the model

By default, all options are disabled.

Options should be set before changing properties.

This property was introduced in Qt 5.14.

Access functions:

QFileSystemModel::Options options() const
void setOptions(QFileSystemModel::Options options)

See also setOption() and testOption().

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:

bool isReadOnly() const
void setReadOnly(bool enable)

This property holds whether the directory model should resolve symbolic links

This is only relevant on Windows.

By default, this property is true.

Access functions:

bool resolveSymlinks() const
void setResolveSymlinks(bool enable)

See also QFileSystemModel::Options.