QTreeView 及 QFileSystemModel使用,QFileSystemModel使用SetRootPath()无效

问题描述

在使用QTreeView制作本地文件浏览功能的时候,发现对QFileSystemModel设置根目录不起作用,即使用setRootPath()后,QTreeView并不显示指定的根目录下面的文件,而是显示主机根目录下的全部文件。
代码片段:

    m_model = new QFileSystemModel(this);

    QString currPath = QDir::currentPath();  //获取当前路径
    m_model->setRootPath(currPath);

    m_treeview = new QTreeView(this);
    m_treeview->setModel(m_model);

实际显示效果,如下图所示:
结果

解决办法

查看文档发现:

QModelIndex QFileSystemModel::setRootPath(const QString &newPath)
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 rootPath().
翻译:
注意:此函数不会更改模型的结构或修改可供视图使用的数据。换句话说,模型的"根"不会更改为在文件系统中由 newPath 指定的目录中仅包含文件和目录。

也就是说使用这个setRootPath()函数后并不改变QTreeView所展示的内容。

在文档中还发现这一段

Detailed Description
The QFileSystemModel class provides a data model for the local filesystem.
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.

所以,使用setRootIndex()函数即可满足需求,

    m_model = new QFileSystemModel(this);

    QString currPath = QDir::currentPath();  //获取当前路径
    m_model->setRootPath(currPath);

    m_treeview = new QTreeView(this);
    m_treeview->setModel(m_model);
    m_treeview->setRootIndex(m_model->index(currPath));

使用效果:
在这里插入图片描述
展示了当前目录下的内容。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值