QFileSystemModel和QTreeView-重置视图时的奇怪行为 [英] QFileSystemModel and QTreeView - strange behavior when resetting view

查看:200
本文介绍了QFileSystemModel和QTreeView-重置视图时的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Qt的官方论坛上写了这篇文章,但它似乎已经死了,所以我将在此处复制粘贴.

I wrote this on official forums of Qt, but it seems dead, so I am going to copy-paste it here.

我正在编写用于复制文件的小程序.我使用QTreeView,并且我已经继承了QFileSystemModel,因此我能够将复选框添加到QTreeView中的每一行.我还使用了与QLineEdit关联的setNameFilters方法,因此用户可以指定他要在QTreeView中显示的文件扩展名.我发现了以下行为:

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. I also use setNameFilters method connected with QLineEdit, so user can specify what file extensions he wants to display in the QTreeView. I have spotted the following behavior:

1)当我运行程序并输入扩展名进行过滤(不触摸QTreeView中的任何节点)时,一切正常,仅显示我提供的带有扩展名的文件(当然还有文件夹).当我更改扩展名并刷新视图时,在我的"C:/"驱动器上,所有内容都会更新,并且仅显示一组新的扩展名.当我扩展以前没有接触过的其他驱动器时,它也可以正确显示文件.

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). 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. When I expand some other drive that I didn’t touch before, it also shows files correctly.

2)当我运行程序并展开时,说我的"C:/""D:/"驱动器,我会看到所有目录和文件(预期的行为).然后,我编写了一些扩展名,并刷新了视图.我展开"C:/"驱动器,一切正常,仅显示具有我提供的扩展名的文件.然后我去"D:/"驱动器,这就是问题所在.它显示所有文件.它忽略了我提供的过滤器.当我打开以前从未打开过的"E:/"驱动器时,文件将像在"C:/"驱动器中一样被正确过滤.

2) When I run the program and expand let say my "C:/" and "D:/" drives I see all directories and files (expected behavior). 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. Then I go to "D:/" drive and here is the problem. 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.

我已经得出结论,这种行为与setRootPath方法有关,因为对于我的QTreeView仅在"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. All other drives that were expanded before change of filters don’t work. Those not expanded work just fine.

问题是:如何使其工作,因此在用户更改过滤器并触发reset()方法之后,整个QTreeView都将被刷新,而不仅仅是根路径和未扩展的元素?也许存在一些根路径,所有驱动器都作为子路径运行,并且可以按预期运行?或者,也许我应该在QTreeView中创建一个名为"MyComputer"的虚拟文件夹,并将其设置为所有驱动器的父文件夹?但是如何获取所有可用驱动器的列表?

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? 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? 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.

修改: 添加一些相关的代码.如果您需要更多,请问.

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
}

推荐答案

尝试将您的根路径更改为我的电脑",而不是C:/.在Windows 7 x64和Qt 4.8.2中,它似乎可以与QFileSystemModel一起使用,但是我不能保证在其他平台上也可以.

Try changing your root path to My Computer instead of 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.

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);

这篇关于QFileSystemModel和QTreeView-重置视图时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆