为什么QFileSystemWatcher在目录中起作用但在Python中不起作用?

问题描述 投票:1回答:2

我从another StackOverflow answer借用了此代码:

from PyQt4 import QtCore

@QtCore.pyqtSlot(str)
def directory_changed(path):
    print('Directory Changed!!!')

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

fs_watcher = QtCore.QFileSystemWatcher(['/path/to/files_1', '/path/to/files_2', '/path/to/files_3'])

fs_watcher.connect(fs_watcher, QtCore.SIGNAL('directoryChanged(QString)'), directory_changed)
fs_watcher.connect(fs_watcher, QtCore.SIGNAL('fileChanged(QString)'), file_changed)

问题是,无论如何,都不会调用file_changed。例如,添加文件时可靠地调用directory_changed,但是更改文件内容不会导致file_changed被调用。

我叫QtCore.SIGNAL('fileChanged(QString)')的一些变体,例如QtCore.SIGNAL('fileChanged(const QString &)'),无济于事。没有警告或错误-它只是不会触发功能。

建议?

python qt pyqt qfilesystemwatcher
2个回答
4
投票

很难确定出了什么问题,因为示例代码不完整,因此根本无法正常工作。


0
投票
import argparse
import configparser
import os
import sys

from PyQt5 import QtCore
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QDesktopWidget, QMessageBox
from PyQt5.QtWidgets import QMainWindow

from ProgressBar_ui import Ui_Form


class ProgressBarWindow(QMainWindow, Ui_Form):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.ui.progressBar.setMinimum(0)
        self.ui.progressBar.setMaximum(0)
        self.ui.progressBar.setValue(0)
        self.setWindowTitle("Progress Bar")
        self.MSversion = ""
        self.LOADING_LOG_PATH = ""
        mainIco = ("Icons\myIcon.ico")
        self.setWindowIcon(QIcon(mainIco))
        self.ui.label.setText("")
        self.ui.label.setWordWrap(True)



    def location_on_the_screen(self):
        ag = QDesktopWidget().availableGeometry()
        sg = QDesktopWidget().screenGeometry()

        widget = self.geometry()
        x = ag.width() - widget.width()
        y = 2 * ag.height() - sg.height() - widget.height()
        self.move(x, y)


    def file_changed(self, pathPassed):
        if os.path.exists(pathPassed):
            f = open(pathPassed, "r")
            for x in f:
                #print(x)
                label =x
                self.ui.label.setText(label)
                if x == "CloseLoading":
                    self.close()

def main():
    app = QApplication(sys.argv)
    w = ProgressBarWindow()
    w.setWindowFlags(w.windowFlags() & ~QtCore.Qt.WindowMaximizeButtonHint)

    parser = argparse.ArgumentParser()
    parser = argparse.ArgumentParser(description='ProgressBar Arguments')

    parser.add_argument('version', action="store", type=str)

    args = vars(parser.parse_args())

    if len(sys.argv) < 1:
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Critical)
        errorMsg = "There are less than 2 command line arguments provided.\nLauncher can not be run.\n\nThe usage is : \nMsLauncher.exe MsVersion -type (LAM/SAW/BAW)(optional)"
        msg.setText(errorMsg)
        msg.setWindowTitle("Save paths..")
        msg.exec_()
        sys.exit()



    p= '/path/toFile/'
    paths = [ p ]

    fs_watcher = QtCore.QFileSystemWatcher(paths)
    #fs_watcher.directoryChanged.connect(w.directory_changed)
    fs_watcher.fileChanged.connect(w.file_changed)
    w.location_on_the_screen()
    w.show()
    app.exec_()

if __name__ == "__main__":
    sys.exit(main())
© www.soinside.com 2019 - 2024. All rights reserved.