Question

I have a pyqt widget that allows a user to make some setting and then save them to a sqlite database, this settings affect watchdog which uses the following code to check changes to the file system

 try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()

The above cannot be placed in the qwidget app code as it will stop some of pyqt code from ever being executed i had tried :

 """ Some other code """
    app = QtGui.QApplication(sys.argv)
    ex = vdrive()
    trayIcon = SystemTrayIcon(QtGui.QIcon("app.xpm"), ex)
    trayIcon.show()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    sys.exit(app.exec_())

How would i integrate watchdog with pyqt to watch the file system changes.

Was it helpful?

Solution

The class watchdog.observers.Observer inherits from threading.Thread, so the actual checking is done in a thread that you start when you call observer.start().

The loop doesn't do anything except letting the program run until the user interrupts it. You can remove it completely and run the Qt event loop as usual:

observer.start()
status = app.exec_()
observer.stop()
observer.join()
sys.exit(status)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top