[英]QFileSystemModel and QTreeView - strange behavior when resetting view
I wrote this on official forums of Qt, but it seems dead, so I am going to copy-paste it here. 我在Qt的官方论坛上写过这个,但它似乎已经死了,所以我要把它复制粘贴到这里。
I am writing small program for copying files. 我正在编写用于复制文件的小程序。 I use QTreeView
and I have inherited from QFileSystemModel
, so I was able to add checkboxes to every row in the QTreeView
. 我使用QTreeView
并且继承自QFileSystemModel
,因此我能够为QTreeView
每一行添加复选框。 I also use setNameFilters
method connected with QLineEdit
, so user can specify what file extensions he wants to display in the QTreeView
. 我还使用与QLineEdit
连接的setNameFilters
方法,因此用户可以指定要在QTreeView
显示的文件扩展名。 I have spotted the following behavior: 我发现了以下行为:
1) When I run the program and enter extensions to filter (without touching any node from the QTreeView
) everything works fine and files with extensions I have provided are only displayed (and folders of course). 1)当我运行程序并输入扩展名进行过滤(不接触QTreeView
任何节点)时,一切正常,我提供的带扩展名的文件只显示(当然还有文件夹)。 When I change the extensions and the view is refreshed, on my "C:/"
drive everything is updated and only new set of extensions is displayed. 当我更改扩展名并刷新视图时,在我的"C:/"
驱动器上,所有内容都会更新,并且只显示一组新的扩展名。 When I expand some other drive that I didn't touch before, it also shows files correctly. 当我展开之前没有触摸的其他驱动器时,它也会正确显示文件。
2) When I run the program and expand let say my "C:/"
and "D:/"
drives I see all directories and files (expected behavior). 2)当我运行程序并展开时,让我说"C:/"
和"D:/"
驱动器,我看到所有目录和文件(预期的行为)。 Then I write some extensions and the view is refreshed. 然后我写了一些扩展,视图刷新。 I expand "C:/"
drive and everything works fine, only files with extensions I have provided are displayed. 我展开"C:/"
驱动器,一切正常,只显示我提供的带扩展名的文件。 Then I go to "D:/"
drive and here is the problem. 然后我去"D:/"
驱动器,这是问题所在。 It displays all files. 它显示所有文件。 It ignores the filters I have provided. 它忽略了我提供的过滤器。 When I open the "E:/"
drive that I have not opened before, the files are filtered correctly as in "C:/"
drive. 当我打开之前没有打开过的"E:/"
驱动器时,文件会在"C:/"
驱动器中正确过滤。
I have concluded, that this behavior has something to do with setRootPath
method, because for my QTreeView
only in "C:/"
drive the filters are working correctly. 我得出结论,这种行为与setRootPath
方法有关,因为对于我的QTreeView
仅在"C:/"
驱动器中,过滤器正常工作。 All other drives that were expanded before change of filters don't work. 在更改过滤器之前展开的所有其他驱动器都不起作用。 Those not expanded work just fine. 那些没有扩展的工作就好了。
The question is: How to get this working, so after user changes the filters and reset() method is fired, the whole QTreeView
is refreshed and not only root path and not-expanded elements? 问题是:如何使这个工作,所以在用户更改过滤器和reset()方法被触发后,整个QTreeView
刷新,而不仅仅是根路径和非扩展元素? Maybe there exists some root path that have all the drives as children and it will work as expected? 也许存在一些将所有驱动器都作为子驱动器的根路径,它将按预期工作? Or maybe I should make some virtual folder in the QTreeView
called "MyComputer"
and set it to be a parent for all the drives? 或者也许我应该在QTreeView
名为"MyComputer"
虚拟文件夹并将其设置为所有驱动器的父文件夹? But how to get list of all the available drives? 但是如何获得所有可用驱动器的列表?
I hope that what I wrote is clear for you and you can help me to get this working. 我希望我写的内容对你来说很清楚,你可以帮助我实现这个目标。
Edit: Adding some code that is relevant. 编辑:添加一些相关的代码。 If you need more just ask. 如果您需要更多,请询问。
//setting up the model and view
QString rPath = "C:/";
rTree_model = new TreeModel(this); //TreeModel inherits from QFileSystemModel
rTree_model->setRootPath(rPath);
ui->rTree->setModel(rTree_model); //applies the model for the qtreeview (ui->rTree)
//(...)
//action when extensions were provided by user
QString extensions = QString(ui->extensionBox->text()); //gets extensions provided by user
QStringList filters;
if(extensions.length() > 0) {
filters = extensions.split(";", QString::SkipEmptyParts); //splits extensions provided with ';' as separator
rTree_model->setNameFilters(filters); //applies filters
ui->rTree->reset(); //resets the view
}
Try changing your root path to My Computer instead of C:/. 尝试将根路径更改为“我的电脑”而不是C:/。 It seems to work with QFileSystemModel in Windows 7 x64 and Qt 4.8.2, but I can't guarantee anything for other platforms. 它似乎适用于Windows 7 x64和Qt 4.8.2中的QFileSystemModel,但我无法保证其他平台的任何功能。
rTree_model = new TreeModel(this);
QString rPath = model->myComputer().toString(); //causes the QFileSystemWatcher to watch every drive?
rTree_model->setRootPath(rPath);
ui->rTree->setModel(rTree_model);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.