QFileSystemModel and QSortFilterProxyModel - root index is not preserved
-
wrote on 28 Jun 2019, 07:10 last edited by
Well, I was sure Qt Champions are capable of:
- creaing a QTreeView instance
- creating a QFileSystemModel instance
- creating a QSortFilterProxyModel instance
- apply a simple filter for the filter model
well then, here is main:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QTreeView treeView; QFileSystemModel navigationModel; navigationModel.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Files); navigationModel.setNameFilterDisables(true); QString root(/*set it somewhere into directory structure, say C:/Qt/some version*/); navigationModel.setRootPath(root); QSortFilterProxyModel filteringModel; filteringModel.setSourceModel(&navigationModel); filteringModel.setRecursiveFilteringEnabled(true); filteringModel.setFilterCaseSensitivity(Qt::CaseInsensitive); treeView.setModel(&filteringModel); treeView.setRootIndex(filteringModel.mapFromSource(navigationModel.index(root))); filteringModel.setFilterFixedString(/*put some folder name which appears deeper in the choosen root*/); treeView.show(); return a.exec(); }
-
Well, I was sure Qt Champions are capable of:
- creaing a QTreeView instance
- creating a QFileSystemModel instance
- creating a QSortFilterProxyModel instance
- apply a simple filter for the filter model
well then, here is main:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QTreeView treeView; QFileSystemModel navigationModel; navigationModel.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Files); navigationModel.setNameFilterDisables(true); QString root(/*set it somewhere into directory structure, say C:/Qt/some version*/); navigationModel.setRootPath(root); QSortFilterProxyModel filteringModel; filteringModel.setSourceModel(&navigationModel); filteringModel.setRecursiveFilteringEnabled(true); filteringModel.setFilterCaseSensitivity(Qt::CaseInsensitive); treeView.setModel(&filteringModel); treeView.setRootIndex(filteringModel.mapFromSource(navigationModel.index(root))); filteringModel.setFilterFixedString(/*put some folder name which appears deeper in the choosen root*/); treeView.show(); return a.exec(); }
@MasterBLB said in QFileSystemModel and QSortFilterProxyModel - root index is not preserved:
Well, I was sure Qt Champions are capable of
You're the one asking for help. It is not about whether Qt champions are capable of doing this or not, it's simply about to help others to help you. You're aware that your list means work for others, right? People in this forum are not paid workers who have to work for you, but just volunteers spending their own time to help others...
-
wrote on 28 Jun 2019, 09:48 last edited by
And that's why I've written the example within around 60s, as requested. I hope it fulfills expectations, but if needed feel free to ask about implementation details of models, though as mentioned there isn't anything related to manipulating root index.
-
Hi
I can confirm it get the same result on Qt5.12.3 and Qt5.9
so at least we can say its not a local bug of sorts.
#include <QApplication> #include <QFileSystemModel> #include <QSortFilterProxyModel> #include <QTreeView> int main(int argc, char *argv[]) { QApplication a(argc, argv); QTreeView treeView; QFileSystemModel navigationModel; navigationModel.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Files); navigationModel.setNameFilterDisables(true); QString root("C:/Qt/5.12.3/msvc2017_64"); navigationModel.setRootPath(root); QSortFilterProxyModel filteringModel; filteringModel.setSourceModel(&navigationModel); filteringModel.setRecursiveFilteringEnabled(true); filteringModel.setFilterCaseSensitivity(Qt::CaseInsensitive); treeView.setModel(&filteringModel); treeView.setRootIndex(filteringModel.mapFromSource(navigationModel.index(root))); filteringModel.setFilterFixedString("Lib"); treeView.show(); return a.exec(); }
-
As @mrjj I can reproduce that on macOS with a recent build of Qt.
You should check the bug report system to see if there's already something related. If not, then please consider opening a new report providing that minimal compilable example.
-
wrote on 28 Jun 2019, 20:10 last edited by
Oh crap, tough luck :/ How to open a Qt bug report?
-
@MasterBLB
Hi
Well you just go there and login with the credentials you use here.
A good read is
https://wiki.qt.io/Reporting_Bugs -
Personally, I didn't. Since you found it, the honour is yours :)
-
Hi
nope, if i had made report, i would have linked url here :)
So please do. -
wrote on 2 Jul 2019, 09:15 last edited by
I don't think it's a bug.
setRootIndex
is a method of the view. The model has no way of knowing (and shouldn't know) what a view is using as root. The filter set on the proxy applies to the whole model. If the index used as root by a view gets filtered out it's only natural for a view to revert to usingQModelIndex()
as root.What you need is a proxy model that implements the
setRootIndex
functionality at the model level. This is on the list of nice-things-I-would-like-to-add-to-Qt-models and it's a fairly easy one to do but I need to find time for it :( -
wrote on 2 Jul 2019, 18:34 last edited by
...and now I'm confused :/
-
wrote on 2 Jul 2019, 18:41 last edited by
@MasterBLB
Confused in what way? I have just read this thread and what @VRonin has written seems correct to me. Are you not understanding what he is saying, or what? -
wrote on 3 Jul 2019, 07:27 last edited by
In the way such behavior breaks SOLID substitute rule - I can't use descendant class QFileSystemModel like it was its base class QAbstractItemModel in QSortFilterProxyModel. While not a 100% bug it's definitely a serious design flaw, and worth to be reported. But where?
-
In the way such behavior breaks SOLID substitute rule - I can't use descendant class QFileSystemModel like it was its base class QAbstractItemModel in QSortFilterProxyModel. While not a 100% bug it's definitely a serious design flaw, and worth to be reported. But where?
@MasterBLB said in QFileSystemModel and QSortFilterProxyModel - root index is not preserved:
But where?
In Qt bug tracker https://bugreports.qt.io/secure/Dashboard.jspa
Also you can talk to Qt developers on their mailing list. -
In the way such behavior breaks SOLID substitute rule - I can't use descendant class QFileSystemModel like it was its base class QAbstractItemModel in QSortFilterProxyModel. While not a 100% bug it's definitely a serious design flaw, and worth to be reported. But where?
wrote on 3 Jul 2019, 08:44 last edited by VRonin 7 Mar 2019, 10:08breaks SOLID
It doesn't. In fact, if it worked as you expected it would break SOLID
it's definitely a serious design flaw
Sorry to be brutal: No, it's not a flaw, you just didn't understand the design of model/views.
To help you get a better idea of what is going on, try use the same model with 2 different treeviews. In only one of the view callsetRootIndex
, and then trigger the filtering. -
wrote on 12 Feb 2025, 22:50 last edited by
Im actually encountering the very same issue.
Clearly it wont be fixed by Qt since Ive even seen posts about this issue back from 2011.
Could someone help me a little bit by telling me what to do please ?@VRonin You were the most precise, suggesting to implements the setRootIndex to the proxymodel, but I would still require a bit more info if I am to do it on my own please :D
-
wrote on 13 Feb 2025, 02:51 last edited by
It looks like @VRonin implemented his idea. See https://github.com/VSRonin/QtModelUtilities/blob/master/READMERootIndexProxyModel.md
-
It looks like @VRonin implemented his idea. See https://github.com/VSRonin/QtModelUtilities/blob/master/READMERootIndexProxyModel.md