Interate over valid items of QFileSystemModel



  • I have a well populated QFileSystemModel and it is visible in QTreeView. In the treeview the user can navigate by different means. Now I would like to provide more navigational help, depending on the content of the files. For this I would like to iterate over all valid items of the current QFileSystemModel and build a new selection. The selection is no problem, but I found no possibility to retrieve something like a QModelIndexList, where i can iterate. Any advice is appreciated.



  • There are no standard iterators for QAbstractItemModels. However, it is not so hard to build one yourself.

    You have to considder in what manner you wish to iterate over the model. There are several ways to do it (breadth first, depth first, and some more specific variations within these). Considder that file systems can get quite deep, or even may contain loops.

    Starting with a specific QModelIndex (or an invalid QModelIndex for the root node), you simply query for the number of rows and columns, and iterate over all of them recursively.



  • Thanks a lot Andre!
    Just one remark: this works only for the populated part of the FileSystemmodel. You'll iterate over the visible elements in the associated View. In my case it is already more then I expected.



  • Of course, but you can force population of the model if you really want that. Still, it is a valid remark.

    I think a generic iterator for QAbstractItemModel would be a nice addition, by the way.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.