QFileSystemModel - 增量更新/抢先更新
来自 Qt 文档:
QFileSystemModel 在调用 setRootPath() 之前不会获取任何文件或目录。这将防止在此之前对文件系统进行任何不必要的查询,例如列出 Windows 上的驱动器。
与 QDirModel(已过时)不同,QFileSystemModel 使用单独的线程来填充自身,因此在查询文件系统时不会导致主线程挂起。调用 rowCount() 将返回 0,直到模型填充目录。 QFileSystemModel 保留文件信息的缓存。使用 QFileSystemWatcher 缓存会自动保持最新。
我将 QTreeView 与使用复选框的子类 QFileSystemModel 一起使用。
如果我在树中展开项目之前调用 QFileSystemModel::rowCount(index),无论是否有任何子目录或文件,我都会收到“0”。但是,一旦展开,再次调用时将给出正确的行计数。
我认为如果您调用 QFileSystemModel::setRootPath() 这将从指定的文件路径获取数据,但在我调用 QFileSystemModel:: 之前它似乎没有“执行得足够快”(缓存未更新)下面我的代码中的 rowCount 。
// Whenever a checkbox in the TreeView is clicked
bool MyModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (role == Qt::CheckStateRole)
{
if (value == Qt::Checked)
{
setRootPath(this->filePath(index));
checklist.insert(index);
set_children(index);
}
else
{
checklist.remove(index);
unchecklist->insert(index);
}
emit dataChanged(index, index);
return true;
}
return QFileSystemModel::setData(index, value, role);
}
// Counts how many items/children the node has (i.e. file/folders)
void MyModel::set_children(const QModelIndex& index)
{
int row = this->rowCount(index);
qDebug() << QString::number(row);
}
在尝试计算该文件夹中包含的项目数之前,有没有办法可以预先收集子文件夹信息?
谢谢
From the Qt documentation:
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(obsolete), 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.
I'm using QTreeView together with a sub-classed QFileSystemModel which uses checkable boxes.
If I call QFileSystemModel::rowCount(index)
before an item has been expanded in the tree I will receive '0', regardless of whether there are any subdirectories or files. However, once it has been expanded the correct row count will then be given when called again.
I think if you call QFileSystemModel::setRootPath() this will fetch the data from the specified file path, but it seems that it does not 'execute fast enough' (the cache is not updated) before I call QFileSystemModel::rowCount
in my code below.
// Whenever a checkbox in the TreeView is clicked
bool MyModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (role == Qt::CheckStateRole)
{
if (value == Qt::Checked)
{
setRootPath(this->filePath(index));
checklist.insert(index);
set_children(index);
}
else
{
checklist.remove(index);
unchecklist->insert(index);
}
emit dataChanged(index, index);
return true;
}
return QFileSystemModel::setData(index, value, role);
}
// Counts how many items/children the node has (i.e. file/folders)
void MyModel::set_children(const QModelIndex& index)
{
int row = this->rowCount(index);
qDebug() << QString::number(row);
}
Is there a way I can pre-emptively gather the sub-folder information before I try to count how many items are contained in that folder?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当收集线程时,QFileSystemModel 发出 directoryLoaded(const QString &path) 信号完成加载目录。
QFileSystemModel emits directoryLoaded(const QString &path) signal, when gather thread finished loading directory.