u/PoopsInUrPee

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?

reddit.com
u/PoopsInUrPee — 6 days ago