Sep-25-2024, 02:21 PM
Hello, i have a Qestion again regarding QThreading. I wrote a very simple Sample Code for a Thread:
MainThread started.
Output Text
For-Loop A: 1
For-Loop A: 2
For-Loop A: 3
For-Loop B: 1
For-Loop B: 2
For-Loop B: 3
Function 2 done.
MainThread ended.
But why is the "Output Text" Line outputted immediately at the beginning of the Code, where i connect the Slot to the Signal in the QThread Class?
I tried it with a Lambda Function: self.thread.print_1.connect(lambda: print("Output Text")). But in this Case nothing happens, there is no Output of the print Function.
My expected output should be:
MainThread started.
For-Loop A: 1
For-Loop A: 2
For-Loop A: 3
Output Text
For-Loop B: 1
For-Loop B: 2
For-Loop B: 3
Function 2 done.
MainThread ended.
because the Emit Function is called after the first For-Loop has endet. It should then Emit and Output the Print-Text and then continue with the second Loop in the Second Function in the Thread. But why it is not working here? What i am doing wrong here or do not understand?
Thank you very much and greetings,
Caliban
from PySide6 import QtCore from PySide6.QtCore import Qt, Signal, QThread import time class MainThread(QThread): def __init__(self): super().__init__() def run(self): print("MainThread started.") self.thread = Testthread() self.thread.print_1.connect(print("Output Text")) self.thread.start() self.thread.wait() print("MainThread ended.") def printtext(self, incoming): print("Func Fertig") print(incoming) class Testthread(QThread): print_1 = Signal() def __init__(self): super().__init__() def run(self): for x in range(1, 4): print("For-Loop A: "+str(x)) time.sleep(1) self.print_1.emit() # Here it should emit to the Print-Function at this Point of the Code. self.func_2() def func_2(self): for x in range(1, 4): print("For-Loop B: "+str(x)) time.sleep(1) print("Function 2 done.") self.quit() if __name__ == "__main__": mainthread = MainThread() mainthread.run()When i run this Code, the Output is:
MainThread started.
Output Text
For-Loop A: 1
For-Loop A: 2
For-Loop A: 3
For-Loop B: 1
For-Loop B: 2
For-Loop B: 3
Function 2 done.
MainThread ended.
But why is the "Output Text" Line outputted immediately at the beginning of the Code, where i connect the Slot to the Signal in the QThread Class?
I tried it with a Lambda Function: self.thread.print_1.connect(lambda: print("Output Text")). But in this Case nothing happens, there is no Output of the print Function.
My expected output should be:
MainThread started.
For-Loop A: 1
For-Loop A: 2
For-Loop A: 3
Output Text
For-Loop B: 1
For-Loop B: 2
For-Loop B: 3
Function 2 done.
MainThread ended.
because the Emit Function is called after the first For-Loop has endet. It should then Emit and Output the Print-Text and then continue with the second Loop in the Second Function in the Thread. But why it is not working here? What i am doing wrong here or do not understand?
Thank you very much and greetings,
Caliban