0

I need to monitor a certain folder and find out whether a new file is added to the folder, after that I ask the user whether they want to print the file out or not. The problem lies with the QFileSystemWatcher, I can't really figure out how to get the name of the file that has been added. I tried this code (see later), but the file_changed function doesn't react to any folder changes. The directory_changed function works.

How can I get the name of the file that has been added and how can I capture the Signal/Boolean, so that when a file is added I can trigger the MessageBox and my System Tray Message (see in code). Thanks a lot and sorry for the poor code, I am new to Qt. And please don't point me to C++ examples, I don't understand them.

 
import sys
from PyQt4.QtGui import *
from PyQt4 import QtCore

# create a 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 a new PayFile.") def show(self): QSystemTrayIcon.show(self) QtCore.QTimer.singleShot(100, self.welcome) # QFileSystemWatcher with 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__': a = QApplication(sys.argv) # add a folder path here fs_watcher = QtCore.QFileSystemWatcher(['C:\\Folder']) # without signals fs_watcher.directoryChanged.connect(directory_changed) # this doesn't work, I don't get the name of the added file fs_watcher.fileChanged.connect(file_changed) # with signals #fs_watcher.connect(fs_watcher, QtCore.SIGNAL('directoryChanged(QString)'), directory_changed) # this doesn't work, I don't get the name of the added file #fs_watcher.connect(fs_watcher, QtCore.SIGNAL('fileChanged(QString)'), file_changed) # how can I find out whether a new file has been added ??? if fs_watcher.directoryChanged.connect(directory_changed): tray = SystemTrayIcon() tray.show() print_msg = "Would you like to do something with the file?" reply = QMessageBox.question(None, 'Print The File', print_msg, QMessageBox.Yes, QMessageBox.No) if reply == QMessageBox.Yes: print "I said yes" # do stuff # print the file if reply == QMessageBox.No: print "No" sys.exit(a.exec_())

1 Answer 1

2

The filesystem watcher only notifies you that a directory has changed, not what has changed in it. Once you are notified, you must iterate the items in the directory and figure out if something interesting has happened there.

It might turn out that the QFileSystemModel would be more useful, as it will emit the rowAdded signals when a directory gets a new member. It already iterates the directory members and determines the changes in spite of filesystem watcher's limitations - it pretty much does what you need to do if you use QFileSystemWatcher directly.

3
  • 1
    Thanks. Could you provide a small example? All I can find is QFileSystemModel with QTreeView and nothing about rowAdded.
    – Ivan Bilan
    Commented Aug 20, 2015 at 7:47
  • @Demiourgos A QFileSystemModel is-a QAbstractItemModel. You'll have to read and understand how models work, otherwise it seems like a lost battle. Commented Aug 20, 2015 at 12:31
  • @KubaOber Here is the example: codereview.stackexchange.com/q/104555/22943 Commented Sep 13, 2015 at 5:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.