Show content of specific subfolders using QFilesystemmodel
-
I'm trying to fill a ListView with items from a QFilesystemmodel. But I want to show only specific filetypes from specific subfolders.
Example: I have a folder structure like that:Images |-2016 | |-events | |-misc |-2017 | |-events | |-misc |-2018 | |-events | |-misc
I set the root for the QFilesystemmodel to "Images" and want to display all the contents of every "events" subfolder as a flat list.
I can filter for the filetypes but I have no clue how to show only the contents of some subfolders. I tried subclassing and modifying the QFilesystemmodel class but couldnt get the needed results. I had a look on QSortfilterproxymodel but dont know if I can get the results with that.
Does anyone have an idea how to solve that? -
Hi and welcome to devnet,
I think you're one the right track. IIRC to flatten the model, you have to re-implement the parent method to return the top level index as parent for all the items you want to show.
-
To flatten the model you can use KDescendantsProxyModel written by one of the stars of Qt Model/Views, Stephen Kelly.
A model that removes the higher hierarchical levels is not currently available but you are not the only one that requested the feature and it's currently on my to-do list for Qt 5.13
-
Thanks for the suggestions so far. I tried to flatten the model by modifying the parent method. I added the following parent method to my model class:
QModelIndex MyModel::parent(const QModelIndex &index) const { QModelIndex grandParent = QFileSystemModel::parent(index); return QFileSystemModel::parent(grandParent); }
In my opinion this should "lift up" the model by one level. But now my list is empty. Without the parent method my list shows the top level of folders (2016, 2017, 2018 from the example).
The description of the KDescendantsProxyModel looks like it does what I need. It seems like a more complex solution and I have currently no idea how I could integrate the class into my project as it has some dependencies with the rest of the KDE framework.
-
@Meta said in Show content of specific subfolders using QFilesystemmodel:
It seems like a more complex solution and I have currently no idea how I could integrate the class into my project as it has some dependencies with the rest of the KDE framework.
See https://forum.qt.io/topic/76533/copy-selected-rows-of-a-table-to-a-model/12