Germar
Germar

Reputation: 446

QFileSystemModel remove folder from Watcher

I have a QFilesystemModel used in a QTreeView. Every time I remove a folder (e.g. through shutils.rmtree()) which is watched by the underlying QFilesystemWatcher I get a this warning

QInotifyFileSystemWatcherEngine::addPaths: inotify_add_watch failed: Datei oder Verzeichnis nicht gefunden
QFileSystemWatcher: failed to add paths: /path/to/deleted/folder

In my mind this should be solved by removing the folder from QFileSystemWatcher before removing it with QFileSystemWatcher.removePath(). But this doesn't work as there seem to be no way to get in touch with the QFileSystemWatcher from a QFileSystemModel (I searched for a solution without luck).

So is there any other way I can tell the QFileSystemModel to stop watching the folder?

PS: I know I could use QFileSystemModel.remove() or .rmdir() which would handle this automaticaly. But that's not an option for me. I need to remove the folder from outside the QFileSystemModel.

I'm using Qt4 with Python3 on Linux.

Upvotes: 1

Views: 1485

Answers (1)

Marcus
Marcus

Reputation: 1713

Firstly, QFileSystemModel and QFileSystemWatcher are two different classes. They are not related in any way what-so-ever. QFileSystemModel does not do the watching, you should add the file watcher yourself.

By the way, do realize the QFileSystemModel::remove just deletes the file. You'll still get the error.

The QFileSystemWatcher is buried very deep beneath the code for QFileSystemModel which is why you cannot access it.

Edit:

However, there is a simple way as to how you can avoid the warning. Here is a code snippet. We are assuming that there is a folder called /tmp/trial with some files in it. This is the folder being viewed, and is deleted externally.

### == Code that creates a warning == ###

# Initial state
fsm = QFileSystemModel()
lv = QListView()
lv.setRootIndex( fsm.setRootPath( "/tmp/trial" ) )

# External deletion of "/tmp/trial", leads to this error
shutils.rmtree( "/tmp/trial" )
# Error
   QInotifyFileSystemWatcherEngine::addPaths: inotify_add_watch failed: file or directory not found
   QFileSystemWatcher: failed to add paths: /tmp/trial


### == Suggested edit == ###

# Initial state
fsm = QFileSystemModel()
lv = QListView()
lv.setRootIndex( fsm.setRootPath( "/tmp/trial" ) )

# Just before deleting
lv.setRootIndex( fsm.setRootPath( "/tmp" ) )

# External deletion of "/tmp/trial", gives no error
# This even look very neat for the user.
shutils.rmtree( "/tmp/trial" )

For the sake of simplicity, I have skipped the proxy model and used a list view, but the procedure be the same in your case with proxy model and tree view.

Upvotes: 2

Related Questions