Getting the path to changed file with QFileSystemWatcher?
Question: From the snippet in [How do I watch a file for changes using
Python?](http://stackoverflow.com/questions/182197/how-do-i-watch-a-file-for-
changes-using-python/5339877#5339877):

    
    
    ...
    @QtCore.pyqtSlot(str)
    def file_changed(path):
        print('File Changed!!!')
    ...
    

I've assumed that the argument `path` of the handler would be, well, the path
of the file that changed (<http://doc.qt.io/qt-4.8/qfilesystemwatcher.html>
~~doesn't really say what should be expected~~ says "`fileChanged` ... signal
is emitted when the file at the specified _path_ is modified, renamed or
removed from disk."). But, then I run the following example (ubuntu 14.04,
python 2.7.6, corresponding py-qt4):

    
    
    import sys, os
    from PyQt4 import QtGui, QtCore
    from PyQt4.QtCore import SIGNAL
    from PyQt4 import Qt
    
    mydir = os.path.dirname( os.path.realpath(__file__) )
    myfile = os.path.join( mydir, "file.dat" )
    print("myfile", myfile)
    
    with open(myfile,"a+") as f:
      f.write("line 1\n")
    
    class MyWindow(QtGui.QWidget):
      def __init__(self):
        global myfile
        QtGui.QWidget.__init__(self)
        self.button = QtGui.QPushButton('Test', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)
        self.fs_watcher = QtCore.QFileSystemWatcher( [myfile] )
        self.fs_watcher.connect(self.fs_watcher, QtCore.SIGNAL('fileChanged(const QString &)'), self.file_changed) # or just 'fileChanged(QString)'
      def handleButton(self):
        print ('Hello World')
      @QtCore.pyqtSlot(str) #
      def file_changed(path):
        print('File Changed!!!' + str(path)) #
    
    if __name__ == '__main__':
      app = QtGui.QApplication(sys.argv)
      window = MyWindow()
      # try to trigger file change here:
      with open(myfile,"a+") as f:
        f.write("line 2\n")
      window.show()
      sys.exit(app.exec_())
    

... and it outputs:

    
    
    ...
    File Changed!!!<__main__.MyWindow object at 0xb5432b24>
    ...
    

So, that argument seems not to receive the path of the changed file, but
instead a reference to the encompassing class?!

So how do I get the path to the file that changed?


Answer: The `file_changed` slot needs a `self` parameter if it is to be a method of
`MyWindow`:

    
    
        @QtCore.pyqtSlot(str)
        def file_changed(self, path):
            print('File Changed!!!' + str(path))
    

