PyQt : Editable QFileSystemModel in a QTreeView

4.8k views Asked by At

I've got this on my file System :

 - myFolder
      - mySubFolder
  1. Within the TreeView I expand the folder "myFolder".
  2. Then I rename it as "myFolder_2".
  3. And finaly I try to rename the folder "mySubFolder" as "mySubFolder_2".

"mySubFolder_2" in is no more considered as a folder but as unknown with a size of -1 bytes and I've got the message : QFileSystemWatcher: failed to add paths: myFolder.

Here is the my source code :

from PyQt4 import QtGui
import sys

app = QtGui.QApplication(sys.argv)
treeView = QtGui.QTreeView()
fileSystemModel = QtGui.QFileSystemModel(treeView)
fileSystemModel.setReadOnly(False)
treeView.setModel(fileSystemModel)
folder = "."
treeView.setRootIndex(fileSystemModel.setRootPath(folder))
treeView.show()
end = app.exec_()

Any help will be welcome.

1

There are 1 answers

5
ekhumoro On

You need to set the root path on the model before setting it on the treeview:

import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
treeView = QtGui.QTreeView()
fileSystemModel = QtGui.QFileSystemModel(treeView)
fileSystemModel.setReadOnly(False)
root = fileSystemModel.setRootPath('.')
treeView.setModel(fileSystemModel)
treeView.setRootIndex(root)
treeView.show()
app.exec_()