PySide2 & Threading... how to set data on widget from thread?
-
Hey
So this is interesting one... in C++ I would just roll with QMetaObject::InvokeMethod(widgetobject,=,t=data{widgetobject.setText(t)},Qt::QueuedConnection);.... but in PySide2... how do I do this ? Or whats the proper way ?
My "other" thread is a different library and runs using native threading.Thread from python and not Qt one...
Hints?
TIA
-
@eyllanesc said in PySide2 & Threading... how to set data on widget from thread?:
@Dariusz Use signals for send information
Yes I have a feeling thats what I need but what is the proper way of doing it ?
class manager(QObject) sendData = Signal(object, str) def __init__(): self.sendData.connect(self.applyData) ### Execute on MAIN Thread def applyData(self, object, data): object.setText(data) ### call from thread XXXX def sendIt(self, someWidget,data): self.sendData.emit(someWidget, data) Like this? How do I know if he uses QueuedConnection or DirectConnection ? TIA
-
@Dariusz said in PySide2 & Threading... how to set data on widget from thread?:
- Do not implement the same logic in the same class so "manager" should only process the data and send it, then connect that signal to the GUI so that it updates the information.
Class Worker(QObject): sendData = Signal(str) def process(self): value = "foo" self.sendData.emit(value)
class GUI(QWidget): def apply_data(self, text): self.foo.setText(text)
worker = Worker() thread = QThread() worker.moveToThread(thread) gui = GUI() worker.sendData.connect(gui.apply_data) QTimer.singleShot(0, worker.process)
- By default in the connection AutoConnection is used so since the sender lives in the second thread and the receiver in the main thread then QueuedConnection will be used.
-
@eyllanesc said in PySide2 & Threading... how to set data on widget from thread?:
@Dariusz said in PySide2 & Threading... how to set data on widget from thread?:
- Do not implement the same logic in the same class so "manager" should only process the data and send it, then connect that signal to the GUI so that it updates the information.
Class Worker(QObject): sendData = Signal(str) def process(self): value = "foo" self.sendData.emit(value)
class GUI(QWidget): def apply_data(self, text): self.foo.setText(text)
worker = Worker() thread = QThread() worker.moveToThread(thread) gui = GUI() worker.sendData.connect(gui.apply_data) QTimer.singleShot(0, worker.process)
- By default in the connection AutoConnection is used so since the sender lives in the second thread and the receiver in the main thread then QueuedConnection will be used.
Hmmm thank you, but will this work with python Thread ? As the worker thread, I got is not Qt...