[PyQt] QFileSystemModel, rename and delete problemes

Jean Dalmayrac jean_dalmayrac at yahoo.fr
Wed Jan 19 17:07:56 GMT 2011


Hi all,
I'm having troubles while trying to rename and delete file using the code below.
For information, the folder "D:/test" contains a subfolder named MyFolder which 
contains a file named MyFile.

Expanding "MyFolder", renaming it as "MyFolder2" and then removing "MyFile" 
gives the message :
QFileSystemWatcher: failed to add paths: D:/test/MyFolder
And MyFile is still in the treeview but not on my file system.

I'm using python 2.6.0 and PyQt 4.5.4

Thanks for the help

Jean

#===========================================================  

import sys
from PyQt4 import QtGui, QtCore
      
class testExplo(QtCore.QObject):
    
    def __init__(self):
        QtCore.QObject.__init__(self)
        
        self.myTreeView = QtGui.QTreeView()
        self.myTreeView.keyPressEvent = self.keyPressEvent
        #-- Model
        self.myModel = QtGui.QFileSystemModel()
        self.myModel.setReadOnly(False)
        self.myModel.setRootPath(r"D:\test")
        
        #-- treeview
        self.myTreeView.setModel(self.myModel)
        
        rootModelIndex = self.myModel.index(r"D:\test")
        self.myTreeView.setRootIndex(rootModelIndex)

        QtCore.QObject.connect(self.myModel,
                               QtCore.SIGNAL("fileRenamed ( const QString, const 

QString , const QString)  "),
                               self.printRenamed)
        
        self.myTreeView.show()
        
  
     
    def printRenamed(self, path, oldName, newName):
        print path, oldName, newName
        self.myModel.revert()



    def keyPressEvent(self,event):
        oModelIndex = self.myTreeView.selectedIndexes()[0]
        if event.key() == QtCore.Qt.Key_Delete:
            self.myModel.remove(oModelIndex)
            event.accept()
            QtGui.QTreeView.keyPressEvent(self.myTreeView, event)
            
try:
    app = QtGui.QApplication(sys.argv)
    myExplo = testExplo()
    end = app.exec_()
except Exception, inst :
    print inst

#===========================================================  



      


More information about the PyQt mailing list