PDA

View Full Version : Best use of QFileSystemWatcher for a custom directory tree



Alundra
15th March 2017, 02:59
Hi,
I have a custom widget to list a multiple directory tree with a root item for each directory using a custom name.
All works very nice and all the needed features is implemented but only one thing is missing which needs some thinking : the file watcher.
How to add the file watcher correctly to manage the real time update of the tree to be like the disk ?
Is it good to add one QFileSystemWatcher on each item of the tree ?
If a parent folder is removed, the children watcher will be called ?
Thanks for the help

anda_skoa
15th March 2017, 09:56
I would use one watcher and handle adding/removing directories as needed.

Though I am wondering if you can't use a QFileSystemModel instead.

Cheers,
_

jefftee
16th March 2017, 00:21
I've read (somewhere) that using QFileSystemWatcher can miss file system changes for a wide array of reasons. From what I recall, the best practice is (if critical that you catch all changes) to use QFileSystemWatcher but also scan the filesystem on a regular interval to ensure you find any changes you may have missed.

QFileSystemWatcher uses the underlying OS's file system notification API's and there are any number of reasons why an app may not be informed of changes, etc. Don't want to scare you off, but just wanted you to be aware of the potential pitfalls, etc.

Alundra
16th March 2017, 03:50
Works fine the method using one QFileSystemWatcher and add all paths to call one Refresh() function.
Only one issue I have actually is if I have one item editing and the refresh is called, the edit action is stopped because the refresh actually clear all and add.
One option is to keep the actual item in edit mode to set him edit again but the best solution is surely to not clear all to add but check what is needed to remove and what is needed to add.
For that I only see the method using 2 pass : First, find what is needed to remove checking if path exists and Second, Add item if not already added.