Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt,QFileSystemWatcher不';无法捕获添加的文件_Python_Qt_Pyqt_Pyside_Qfilesystemwatcher - Fatal编程技术网

Python PyQt,QFileSystemWatcher不';无法捕获添加的文件

Python PyQt,QFileSystemWatcher不';无法捕获添加的文件,python,qt,pyqt,pyside,qfilesystemwatcher,Python,Qt,Pyqt,Pyside,Qfilesystemwatcher,我需要监视某个文件夹,并确定是否将新文件添加到该文件夹中,然后询问用户是否要打印该文件。问题在于QFileSystemWatcher,我真的不知道如何获取已添加文件的名称。我尝试了这段代码(见下文),但file_changed函数对任何文件夹更改都没有反应。目录更改功能可以工作 如何获取已添加文件的名称以及如何捕获信号/布尔值,以便在添加文件时触发MessageBox和系统托盘消息(请参见代码中的)。非常感谢并为糟糕的代码感到抱歉,我是Qt新手。请不要把我指向C++例子,我不理解它们。 文件系

我需要监视某个文件夹,并确定是否将新文件添加到该文件夹中,然后询问用户是否要打印该文件。问题在于QFileSystemWatcher,我真的不知道如何获取已添加文件的名称。我尝试了这段代码(见下文),但file_changed函数对任何文件夹更改都没有反应。目录更改功能可以工作

如何获取已添加文件的名称以及如何捕获信号/布尔值,以便在添加文件时触发MessageBox和系统托盘消息(请参见代码中的)。非常感谢并为糟糕的代码感到抱歉,我是Qt新手。请不要把我指向C++例子,我不理解它们。

文件系统监视程序只通知您目录已更改,而不是其中更改的内容。收到通知后,必须迭代目录中的项目,并确定是否发生了有趣的事情


结果可能是
QFileSystemModel
更有用,因为当目录获得新成员时,它将发出
rowAdded
信号。它已经迭代了目录成员并确定了更改,尽管filesystem watcher有其局限性—如果您直接使用
QFileSystemWatcher
,它几乎完成了您需要做的事情。

谢谢。你能举个小例子吗?我所能找到的只有QFileSystemModel和QTreeView,没有添加任何关于行的内容。@Demiourgos A
QFileSystemModel
is-A
QAbstractItemModel
。你必须阅读并理解模型是如何工作的,否则这看起来像是一场失败的战斗。@KubaOber以下是一个例子: 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_())