QFileSystemModel Class

  • Header: QFileSystemModel

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Gui)

    target_link_libraries(mytarget PRIVATE Qt6::Gui)

  • qmake: QT += gui

  • Inherits: QAbstractItemModel

  • Group: QFileSystemModel is part of model-view

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().

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:

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

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

 
Sélectionnez
    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:

 
Sélectionnez
    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