本页内容

QFileSystemModel Class

QFileSystemModel 类为本地文件系统提供了一个数据模型。更多内容...

头文件: #include <QFileSystemModel>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承自: QAbstractItemModel

公共类型

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

属性

公共函数

QFileSystemModel(QObject *parent = nullptr)
virtual ~QFileSystemModel()
QIcon fileIcon(const QModelIndex &index) const
QFileInfo fileInfo(const QModelIndex &index) const
QString fileName(const QModelIndex &index) const
QString filePath(const QModelIndex &index) const
QDir::Filters filter() const
QAbstractFileIconProvider *iconProvider() const
QModelIndex index(const QString &path, int column = 0) const
bool isDir(const QModelIndex &index) const
bool isReadOnly() const
QDateTime lastModified(const QModelIndex &index) const
(since 6.6) QDateTime lastModified(const QModelIndex &index, const QTimeZone &tz) const
QModelIndex mkdir(const QModelIndex &parent, const QString &name)
QVariant myComputer(int role = Qt::DisplayRole) const
bool nameFilterDisables() const
QStringList nameFilters() const
QFileSystemModel::Options options() const
QFileDevice::Permissions permissions(const QModelIndex &index) const
bool remove(const QModelIndex &index)
bool resolveSymlinks() const
bool rmdir(const QModelIndex &index)
QDir rootDirectory() const
QString rootPath() const
void setFilter(QDir::Filters filters)
void setIconProvider(QAbstractFileIconProvider *provider)
void setNameFilterDisables(bool enable)
void setNameFilters(const QStringList &filters)
void setOption(QFileSystemModel::Option option, bool on = true)
void setOptions(QFileSystemModel::Options options)
void setReadOnly(bool enable)
void setResolveSymlinks(bool enable)
QModelIndex setRootPath(const QString &newPath)
qint64 size(const QModelIndex &index) const
bool testOption(QFileSystemModel::Option option) const
QString type(const QModelIndex &index) const

重新实现的公共函数

virtual bool canFetchMore(const QModelIndex &parent) const override
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
virtual void fetchMore(const QModelIndex &parent) override
virtual Qt::ItemFlags flags(const QModelIndex &index) const override
virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const override
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
virtual QMimeData *mimeData(const QModelIndexList &indexes) const override
virtual QStringList mimeTypes() const override
virtual QModelIndex parent(const QModelIndex &index) const override
virtual QHash<int, QByteArray> roleNames() const override
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override
virtual bool setData(const QModelIndex &idx, const QVariant &value, int role = Qt::EditRole) override
virtual QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override
virtual Qt::DropActions supportedDropActions() const override

信号

void directoryLoaded(const QString &path)
void fileRenamed(const QString &path, const QString &oldName, const QString &newName)
void rootPathChanged(const QString &newPath)

重新实现的受保护函数

virtual bool event(QEvent *event) override
virtual void timerEvent(QTimerEvent *event) override

详细说明

该类提供了对本地文件系统的访问,包含用于重命名和删除文件及目录,以及创建新目录的函数。在最简单的情况下,它可与合适的显示控件配合使用,作为浏览器或过滤器的一部分。

可以通过QAbstractItemModel 提供的标准接口访问QFileSystemModel,但它还提供了一些特定于目录模型的便捷函数。fileInfo()、isDir()、fileName()和filePath()函数提供与模型中项目相关的底层文件和目录的信息。可以使用mkdir()和rmdir()创建和删除目录。

使用示例

一个用于显示默认目录内容的目录模型通常通过父对象构建:

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

可以使用树形视图来显示模型的内容

QTreeView *tree = new QTreeView(splitter);
tree->setModel(model);

通过设置树视图的根索引,可以显示特定目录的内容:

tree->setRootIndex(model->index(QDir::currentPath()));

视图的根索引可用于控制分层模型中显示的范围。QFileSystemModel 提供了一个便捷函数,该函数会针对模型中某个目录的路径返回一个合适的模型索引。

缓存与性能

QFileSystemModel 使用单独的线程来填充自身数据,因此查询文件系统时不会导致主线程挂起。调用 `rowCount()` 将返回 `0 `,直到模型填充完该目录为止。包含 QFileSystemModel 的线程需要运行一个事件循环来处理传入的数据。

在调用setRootPath() 之前,QFileSystemModel 不会开始加载自身数据。这可防止在此之前对系统根文件系统进行任何不必要的查询,例如在 Windows 上枚举驱动器。

QFileSystemModel 维护一个包含文件信息的缓存。该缓存会通过QFileSystemWatcher 自动保持最新状态。

另请参阅 “模型类”。

成员类型文档

enum QFileSystemModel::Option
flags QFileSystemModel::Options

常数值描述
QFileSystemModel::DontWatchForChanges0x00000001不要在路径中添加文件监视器。这在使用该模型处理行编辑补全等简单任务时,可减少开销。
QFileSystemModel::DontResolveSymlinks0x00000002不要在文件系统模型中解析符号链接。默认情况下,符号链接会被解析。
QFileSystemModel::DontUseCustomDirectoryIcons0x00000004始终使用默认目录图标。某些平台允许用户设置不同的图标。自定义图标查找会在网络或可移动驱动器上造成较大的性能影响。这将相应地在图标提供程序中设置 QFileIconProvider::DontUseCustomDirectoryIcons 选项。

Options 类型是QFlags<Option> 的 typedef。它存储 Option 值的或运算组合。

另请参阅 resolveSymlinks 。

enum QFileSystemModel::Roles

常数值描述
QFileSystemModel::FileIconRoleQt::DecorationRole 
QFileSystemModel::FilePathRoleQt::UserRole + 1 
QFileSystemModel::FileNameRoleQt::UserRole + 2 
QFileSystemModel::FilePermissionsQt::UserRole + 3 
QFileSystemModel::FileInfoRoleQt::FileInfoRole索引对应的QFileInfo 对象

属性文档

nameFilterDisables : bool

此属性用于确定未通过名称筛选的文件是处于隐藏状态还是禁用状态

该属性默认值为true

访问函数:

bool nameFilterDisables() const
void setNameFilterDisables(bool enable)

options : Options

该属性包含影响模型的各种选项

默认情况下,所有选项均处于禁用状态。

在更改属性之前,应先设置选项。

访问函数:

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

另请参阅 setOption() 和testOption()。

readOnly : bool

该属性控制目录模型是否允许向文件系统写入数据

如果将此属性设置为 false,则目录模型将允许对文件和目录进行重命名、复制和删除操作。

该属性的默认值为true

访问函数:

bool isReadOnly() const
void setReadOnly(bool enable)

该属性用于确定目录模型是否应解析符号链接

这仅在 Windows 系统上有效。

默认情况下,此属性的值为true 。

访问函数:

bool resolveSymlinks() const
void setResolveSymlinks(bool enable)

另请参阅 QFileSystemModel::Options 。

成员函数文档

[explicit] QFileSystemModel::QFileSystemModel(QObject *parent = nullptr)

根据给定的parent 构建一个文件系统模型。

[virtual noexcept] QFileSystemModel::~QFileSystemModel()

销毁此文件系统模型。

[override virtual] bool QFileSystemModel::canFetchMore(const QModelIndex &parent) const

重写了:QAbstractItemModel::canFetchMore(const QModelIndex &parent) const。

[override virtual] int QFileSystemModel::columnCount(const QModelIndex &parent = QModelIndex()) const

重写了:QAbstractItemModel::columnCount(const QModelIndex &parent) const。

[override virtual] QVariant QFileSystemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const

重写了:QAbstractItemModel::data(const QModelIndex &index, int role) const。

另请参阅 setData()。

[signal] void QFileSystemModel::directoryLoaded(const QString &path)

当聚合线程完成path 的加载时,会发出此信号。

[override virtual] bool QFileSystemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)

重写自:QAbstractItemModel::dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)。

处理由拖放操作提供的data ,该操作以action 在由row 和column 以及parent 索引指定的模型行上结束。若操作成功,则返回true。

另请参阅 supportedDropActions()。

[override virtual protected] bool QFileSystemModel::event(QEvent *event)

重写了:QObject::event(QEvent *e)。

[override virtual] void QFileSystemModel::fetchMore(const QModelIndex &parent)

重写了:QAbstractItemModel::fetchMore(const QModelIndex &parent)。

QIcon QFileSystemModel::fileIcon(const QModelIndex &index) const

返回存储在模型中、位于指定index 下的项的图标。

QFileInfo QFileSystemModel::fileInfo(const QModelIndex &index) const

返回存储在模型中、位于给定index 下的项的QFileInfo 。

QString QFileSystemModel::fileName(const QModelIndex &index) const

返回存储在模型中、位于指定index 下的项的文件名。

QString QFileSystemModel::filePath(const QModelIndex &index) const

返回在给定的index 下,模型中存储的项的路径。

[signal] void QFileSystemModel::fileRenamed(const QString &path, const QString &oldName, const QString &newName)

每当文件oldName 成功重命名为newName 时,都会发出此信号。该文件位于目录path 中。

QDir::Filters QFileSystemModel::filter() const

返回为目录模型指定的过滤器。

如果未设置过滤器,则默认过滤器为QDir::AllEntries |QDir::NoDotAndDotDot |QDir::AllDirs 。

另请参阅 setFilter() 和QDir::Filters 。

[override virtual] Qt::ItemFlags QFileSystemModel::flags(const QModelIndex &index) const

重写了:QAbstractItemModel::flags(const QModelIndex &index) const。

[override virtual] bool QFileSystemModel::hasChildren(const QModelIndex &parent = QModelIndex()) const

重写了:QAbstractItemModel::hasChildren(const QModelIndex &parent) const。

[override virtual] QVariant QFileSystemModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const

重写了:QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role) const。

QAbstractFileIconProvider *QFileSystemModel::iconProvider() const

返回此目录模型的文件图标提供程序。

另请参阅 setIconProvider()。

[override virtual] QModelIndex QFileSystemModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const

重写了:QAbstractItemModel::index(int row, int column, const QModelIndex &parent) const。

QModelIndex QFileSystemModel::index(const QString &path, int column = 0) const

返回给定path 和column 对应的模型项索引。

这是一个重载函数。

bool QFileSystemModel::isDir(const QModelIndex &index) const

如果模型项index 表示一个目录,则返回true ;否则返回false 。

QDateTime QFileSystemModel::lastModified(const QModelIndex &index) const

返回index 文件的最后修改日期和时间(以本地时间为准)。

这是一个重载函数,相当于调用:

lastModified(index, QTimeZone::LocalTime);

如果index 无效,则返回一个默认构造的QDateTime 。

[since 6.6] QDateTime QFileSystemModel::lastModified(const QModelIndex &index, const QTimeZone &tz) const

返回index 最后一次修改的日期和时间(以时区tz 为基准)。

tz 的典型参数包括QTimeZone::UTC 或QTimeZone::LocalTime 。由于 UTC 无需对原生文件系统 API 返回的时间进行任何转换,因此获取 UTC 时间可能更快。若需向用户显示时间,通常选择 LocalTime。

如果 `index ` 无效,则返回一个默认构造的 `QDateTime `。

该函数于 Qt 6.6 版本中引入。

[override virtual] QMimeData *QFileSystemModel::mimeData(const QModelIndexList &indexes) const

重写:QAbstractItemModel::mimeData(const QModelIndexList &indexes) const。

返回一个包含指定indexes 序列化描述的对象。用于描述与索引对应的项的格式由mimeTypes() 函数获取。

如果索引列表为空,则返回nullptr ,而不是序列化的空列表。

[override virtual] QStringList QFileSystemModel::mimeTypes() const

重新实现了:QAbstractItemModel::mimeTypes() const。

返回一个 MIME 类型列表,该列表可用于描述模型中的项目列表。

QModelIndex QFileSystemModel::mkdir(const QModelIndex &parent, const QString &name)

在parent 模型索引目录下创建一个名为name 的目录。

QVariant QFileSystemModel::myComputer(int role = Qt::DisplayRole) const

返回存储在指定role 下的“我的电脑”项的相关数据。

另请参阅 Qt::ItemDataRole 。

QStringList QFileSystemModel::nameFilters() const

返回一个应用于模型中名称的过滤器列表。

另请参阅 setNameFilters()。

[override virtual] QModelIndex QFileSystemModel::parent(const QModelIndex &index) const

重写了:QAbstractItemModel::parent(const QModelIndex &index) const。

QFileDevice::Permissions QFileSystemModel::permissions(const QModelIndex &index) const

返回index 的完整“或”运算组合的QFile::Permission对象。

bool QFileSystemModel::remove(const QModelIndex &index)

从文件系统模型中移除模型项index ,并从文件系统中删除相应的文件;若操作成功,则返回true。如果无法移除该项,则返回false。

警告:此 函数会从文件系统中删除文件;不会将文件移动到可恢复的位置。

另请参阅 rmdir()。

bool QFileSystemModel::rmdir(const QModelIndex &index)

从文件系统模型中移除与模型项index 对应的目录,并从文件系统中删除该目录;若操作成功,则返回true。若无法移除该目录,则返回false。

警告:此 函数会从文件系统中删除目录;不会将其移动到可恢复的位置。

另请参阅 remove()。

[override virtual] QHash<int, QByteArray> QFileSystemModel::roleNames() const

重新实现了:QAbstractItemModel::roleNames() const。

QDir QFileSystemModel::rootDirectory() const

当前设置的目录

另请参阅 rootPath()。

QString QFileSystemModel::rootPath() const

当前设置的根路径

另请参阅 setRootPath() 和rootDirectory()。

[signal] void QFileSystemModel::rootPathChanged(const QString &newPath)

每当根路径更改为newPath 时,都会发出此信号。

[override virtual] int QFileSystemModel::rowCount(const QModelIndex &parent = QModelIndex()) const

重写了:QAbstractItemModel::rowCount(const QModelIndex &parent) const。

[override virtual] bool QFileSystemModel::setData(const QModelIndex &idx, const QVariant &value, int role = Qt::EditRole)

重写了:QAbstractItemModel::setData (const QModelIndex &index, const QVariant &value, int role)。

另请参阅 data()。

void QFileSystemModel::setFilter(QDir::Filters filters)

将目录模型的过滤器设置为由filters 指定的值。

请注意,您设置的过滤器必须始终包含QDir::AllDirs 枚举值,否则QFileSystemModel 将无法读取目录结构。

另请参阅 filter() 和QDir::Filters 。

void QFileSystemModel::setIconProvider(QAbstractFileIconProvider *provider)

设置目录模型中文件图标的provider 。

另请参阅 iconProvider()。

void QFileSystemModel::setNameFilters(const QStringList &filters)

将名称filters 设置为对现有文件进行处理。

另请参阅 nameFilters()。

void QFileSystemModel::setOption(QFileSystemModel::Option option, bool on = true)

如果on 为true,则启用指定的option ;否则,清除指定的option 。

在更改属性之前应先设置选项。

另请参阅 options 和testOption()。

QModelIndex QFileSystemModel::setRootPath(const QString &newPath)

通过在该目录上安装一个file system watcher ,将模型监视的目录设置为newPath 。该目录内的文件和目录的任何更改都将反映在模型中。

如果路径发生变化,将触发rootPathChanged() 信号。

注意:此 函数不会更改模型的结构,也不会修改视图可用的数据。换言之,模型的“根目录”不会被更改为仅包含文件系统中由newPath 指定的目录内的文件和目录。

另请参阅 rootPath()、QTreeView::setRootIndex() 和TreeView::rootIndex 。

[override virtual] QModelIndex QFileSystemModel::sibling(int row, int column, const QModelIndex &idx) const

重写了:QAbstractItemModel::sibling(int row, int column, const QModelIndex &index) const。

qint64 QFileSystemModel::size(const QModelIndex &index) const

返回index 的字节大小。如果文件不存在,则返回0。

[override virtual] void QFileSystemModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder)

重写了:QAbstractItemModel::sort(int column, Qt::SortOrder order)。

[override virtual] Qt::DropActions QFileSystemModel::supportedDropActions() const

重新实现了:QAbstractItemModel::supportedDropActions() const。

bool QFileSystemModel::testOption(QFileSystemModel::Option option) const

如果给定的option 已启用,则返回true ;否则,返回false。

另请参阅 options 和setOption()。

[override virtual protected] void QFileSystemModel::timerEvent(QTimerEvent *event)

重写了:QObject::timerEvent(QTimerEvent *event)。

QString QFileSystemModel::type(const QModelIndex &index) const

返回文件index 的类型,例如“目录”或“JPEG文件”。

© 2026 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.