PyQt,QFileSystemWatcher 不捕获添加的文件
PyQt, QFileSystemWatcher doesn't capture the file added
我需要监控某个文件夹,查看是否有新文件添加到文件夹中,然后询问用户是否要打印文件。问题在于 QFileSystemWatcher,我真的不知道如何获取已添加文件的名称。我试过这段代码(见后文),但 file_changed 函数对任何文件夹更改都没有反应。 directory_changed 函数有效。
如何获取已添加文件的名称以及如何捕获 Signal/Boolean,以便在添加文件时触发 MessageBox 和我的系统托盘消息(请参阅代码)。非常感谢,对于糟糕的代码感到抱歉,我是 Qt 的新手。请不要向我指出 C++ 示例,我不理解它们。
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_())
文件系统观察器只通知您目录已更改,而不是其中发生了什么更改。收到通知后,您必须遍历目录中的项目并弄清楚那里是否发生了一些有趣的事情。
结果可能 QFileSystemModel
会更有用,因为它会在目录获得新成员时发出 rowAdded
信号。尽管存在文件系统观察器的限制,它已经迭代目录成员并确定更改 - 如果您直接使用 QFileSystemWatcher
,它几乎可以完成您需要做的事情。
我需要监控某个文件夹,查看是否有新文件添加到文件夹中,然后询问用户是否要打印文件。问题在于 QFileSystemWatcher,我真的不知道如何获取已添加文件的名称。我试过这段代码(见后文),但 file_changed 函数对任何文件夹更改都没有反应。 directory_changed 函数有效。
如何获取已添加文件的名称以及如何捕获 Signal/Boolean,以便在添加文件时触发 MessageBox 和我的系统托盘消息(请参阅代码)。非常感谢,对于糟糕的代码感到抱歉,我是 Qt 的新手。请不要向我指出 C++ 示例,我不理解它们。
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_())
文件系统观察器只通知您目录已更改,而不是其中发生了什么更改。收到通知后,您必须遍历目录中的项目并弄清楚那里是否发生了一些有趣的事情。
结果可能 QFileSystemModel
会更有用,因为它会在目录获得新成员时发出 rowAdded
信号。尽管存在文件系统观察器的限制,它已经迭代目录成员并确定更改 - 如果您直接使用 QFileSystemWatcher
,它几乎可以完成您需要做的事情。