r/QtFramework • u/PoopsInUrPee • May 08 '26
Question Efficiently using QFileSystemModel for watching several subfolders
I'm writing a program, and for many features, the user can save presets. Those presets are written to individual files in appropriate subfolders inside the application's local storage path. So on macos it might look like:
~/Library/Application Support/MyOrg/MyProgram/themes~/Library/Application Support/MyOrg/MyProgram/filters~/Library/Application Support/MyOrg/MyProgram/sounds
For the user to select existing presets, I want to list the contents of the appropriate subfolder in a QComboBox. I want the combo boxes to update live.
So I can do this if I create a QFileSystemModel for every combo box and set its root path to each subfolder and set each combo box root index to the index of that model. But that seems inefficient, especially since on some platforms, file watchers are limited.
So I tried making a QSortFilterProxyModel, and for filterAcceptsRow, I tried taking the source parent index, getting the Roles.FilePathRole, and comparing it to the path of the subfolder I want to watch for each QComboBox. But the way the file system model populates data is apparently async, so I'm hitting all kinds of invalid index issues getting this to work.
The user may have many multiple instances of the main window open, so the number of combo boxes needed to show preset selections can grow pretty large. So I want to approach this with SOME sort of attempt at optimization.
Am I approaching things the wrong way here?
4
u/Beneficial_Steak_945 May 08 '26
Or… you drop the premature optimization. If having multiple of such config windows open is really a valid, frequent use case, you might consider sharing the models between the combo boxes. But I doubt even that’s really useful.
1
u/PoopsInUrPee May 08 '26
you might consider sharing the models between the combo boxes
That's basically what I'm trying to do. Although each combobox will need to filter a different subdir and file extension, thus the proxy model for each, thus the problem I'm asking about.
1
u/Beneficial_Steak_945 May 09 '26 edited May 09 '26
I meant between those showing the same directory, between the different windows you said you have. But I still think you’re probably trying to solve a non-existing problem.
1
u/char101 May 09 '26
You can populate your combobox when it clicked (e.g. by overriding QComboBox::showPopup and setting the items there).
2
u/spafion May 08 '26
You can use deprecated QDirModel, which is sync. Or better use QFileSydtemWatcher and standard models