0

Hello I have two function. One Class is a watchdog and the other class is a pyqt gui. I have three QTextBrowser. If I connect a button to the function in the QT Class and call the function my TextBrowser get updated normally. If I trigger an event in the watchdog and the watchdog class calls my QT class, my TextBrowser didnt update.

This is how I call the function from another class

class Handler(FileSystemEventHandler):

@staticmethod
def on_any_event(event):
    if event.is_directory:
        return None

    elif event.event_type == 'created':
        # Event is created, you can process it now
        print("Watchdog received created event - % s." % event.src_path)
        window = MyWindow()
        window.start_xl()

This is the function, which works when i call it with a button.

    def update(self):
    terminate('cmd.exe')
    terminate('EXCEL.exe')

    self.text_Browser = self.ui.textBrowser
    self.text_Browser_2 = self.ui.textBrowser_2
    self.text_Browser_3 = self.ui.textBrowser_3

    self.text_Browser.setTextColor(QtGui.QColor('white'))
    self.text_Browser_2.setTextColor(QtGui.QColor('white'))
    self.text_Browser_3.setTextColor(QtGui.QColor('white'))

    path = "C:/Program Files (x86)/STIHL/Faps/Temp"
    # Check if the folder is empty
    if os.listdir(path):
        # Get a list of all files in the directory
        files = os.listdir(path)
        # The first file in the directory
        first_file = files[0]
        self.text_Browser.append("FINISHED")
        self.text_Browser_2.append("goal_dir")
        self.text_Browser_3.append(first_file)

    else:
        self.ui.label.setText("Ordner ist Leer")
        pass

This is the function which is called by the class Handler. Everything is working besides the updating of the TextBrowser

def start_xl(self):

    terminate('cmd.exe')
    terminate('EXCEL.exe')

    self.text_Browser = self.ui.textBrowser
    self.text_Browser_2 = self.ui.textBrowser_2

    self.text_Browser.setTextColor(QtGui.QColor('white'))
    self.text_Browser_2.setTextColor(QtGui.QColor('white'))

    print(self.text_Browser)
    print(self.text_Browser_2)


        # QtCore.QMetaObject.invokeMethod(self.text_Browser, "append", QtCore.Qt.QueuedConnection,
        #                                 QtCore.Q_ARG(str, "FINISHED"))
        # QtCore.QMetaObject.invokeMethod(self.text_Browser_2, "append", QtCore.Qt.QueuedConnection,
        #                                 QtCore.Q_ARG(str, "goal_dir"))

        self.text_Browser.setPlainText("FINISHED")
        # self.text_Browser_2.append("goal_dir")

I tried it with invoke and append. The kill process get started, so the function is called. Is there something I should consider?

  • Your code is a bit confusing, since you split it in multiple parts and also didn't check its indentation in the post preview (read more about [formatting code](//meta.stackoverflow.com/a/251362) and always check the preview before submitting). That said, watchdog works on a separate thread, and UI elements are ***NOT*** thread-safe: you shall **never** try to access them from external threads, especially for setting attributes or properties. Use QThread and signals. – musicamante Feb 10 '23 at 02:45
  • Yes, you are right. I tested it. When I run the function from the normal Thread it works, when i run the function from an external thread it doesnt work. How can I fix it or how can I let the last part run from my original Thread and switch again to the external Thread? – Stefan1996t Feb 10 '23 at 02:58
  • As said, you *must* use signals. If you don't know what signals are, please do some studying (starting from the [official documentation](https://doc.qt.io/qt-5/signalsandslots.html) and following some [tutorials like this](https://zetcode.com/gui/pyqt5/eventssignals/)), then read more about using [QThread](https://realpython.com/python-pyqt-qthread/) (note that the linked tutorial uses a QObject subclass moved to a thread, but you can also subclass QThread and override `run()` depending on your needs). Be patient, multithreading is not an immediate subject, and Qt is a quite complex toolkit. – musicamante Feb 10 '23 at 03:15

0 Answers0