python - PyQt, QFileSystemWatcher doesn't capture the file added -
i need monitor folder , find out whether new file added folder, after ask user whether want print file out or not. problem lies qfilesystemwatcher, can't figure out how name of file has been added. tried code (see later), file_changed function doesn't react folder changes. directory_changed function works.
how can name of file has been added , how can capture signal/boolean, when file added can trigger messagebox , system tray message (see in code). lot , sorry poor code, new qt. , please don't point me c++ examples, don't understand them.
import sys pyqt4.qtgui import * pyqt4 import qtcore # create system tray message class systemtrayicon(qsystemtrayicon): def __init__(self, parent=none): qsystemtrayicon.__init__(self, parent) #self.seticon(qicon.fromtheme("document-save")) self.seticon(qicon("c:\icon2.ico")) def welcome(self): self.showmessage("new payfile found!", "we found new payfile.") def show(self): qsystemtrayicon.show(self) qtcore.qtimer.singleshot(100, self.welcome) # qfilesystemwatcher signals @qtcore.pyqtslot(str) def directory_changed(path): print('directory changed: ', path) @qtcore.pyqtslot(str) def file_changed(path): print('file changed: ', path) ''' # qfilesystemwatcher without signals def directory_changed(path): print('directory changed: %s' % path) def file_changed(path): print('file changed: %s' % path) ''' if __name__ == '__main__': = qapplication(sys.argv) # add folder path here fs_watcher = qtcore.qfilesystemwatcher(['c:\\folder']) # without signals fs_watcher.directorychanged.connect(directory_changed) # doesn't work, don't name of added file fs_watcher.filechanged.connect(file_changed) # signals #fs_watcher.connect(fs_watcher, qtcore.signal('directorychanged(qstring)'), directory_changed) # doesn't work, don't name of added file #fs_watcher.connect(fs_watcher, qtcore.signal('filechanged(qstring)'), file_changed) # how can find out whether new file has been added ??? if fs_watcher.directorychanged.connect(directory_changed): tray = systemtrayicon() tray.show() print_msg = "would file?" reply = qmessagebox.question(none, 'print file', print_msg, qmessagebox.yes, qmessagebox.no) if reply == qmessagebox.yes: print "i said yes" # stuff # print file if reply == qmessagebox.no: print "no" sys.exit(a.exec_())
the filesystem watcher notifies directory has changed, not what has changed in it. once notified, must iterate items in directory , figure out if interesting has happened there.
it might turn out qfilesystemmodel
more useful, emit rowadded
signals when directory gets new member. iterates directory members , determines changes in spite of filesystem watcher's limitations - pretty need if use qfilesystemwatcher
directly.
Comments
Post a Comment