QFileSystemModel and QTreeView - strange behavior when resetting view

妖精的绣舞 提交于 2020-01-06 08:14:18

问题


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

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

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.

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.

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
}

回答1:


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


来源:https://stackoverflow.com/questions/17261338/qfilesystemmodel-and-qtreeview-strange-behavior-when-resetting-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!