Is this safe? Do I have to use signals to call UI widget methods?
-
Is this safe? Do I have to use signals to call UI widget methods on the main thread?
import sys from PySide2.QtCore import QThread from PySide2.QtWidgets import QApplication, QWidget class MyThread(QThread): def __init__(self, main_window, *args, **kwargs): super().__init__(*args, **kwargs) self.main_window = main_window def run(self): # Is this safe? Do I have to use signals to call UI widget methods? print(self.main_window.geometry()) # Is this safe? Do I have to use signals to call UI widget methods? self.main_window.hide() print(self.parent()) if __name__ == '__main__': app = QApplication(sys.argv) my_widget = QWidget() my_widget.show() my_thread = MyThread(my_widget) my_thread.start() sys.exit(app.exec_())
refer link:
https://stackoverflow.com/questions/53058849/is-it-ok-to-read-qt-widgets-from-another-thread
https://stackoverflow.com/questions/42876085/is-it-possible-to-hide-qt-widget-window-from-other-thread -
Hi
No its not safe.
Say your thread reads main_window.geometry() at the exact same time
you resize the window and the main thread updates the info.Its best to use signals and slots for this.
Else its just a crash waiting to happen.
-
-
Technically: QWidget based objects should only be manipulated in the same thread where the QApplication object was created. Usually, it's in the main thread, the one created to run the "main" function but it can be another one although it's not the most used technique.
-
Did you actually take the time to fully understand what I wrote ? At no point did I say that you could use a QWidget in a secondary thread beside the main thread, quite the contrary. I wrote that the thread creating the QApplication object was the only one where you are allowed to modify QWidget objects and that this precise thread might be a secondary thread even if it's not the common use case.
-
This post is deleted!