Jun-08-2018, 03:09 PM
Hi all, I'm working in a Python application, using PySide on a Raspberry Pi, with Raspbian Jessie OS.
I'm very new in Python, so I'm not an expert at all, and probably it's a begginer's question.
In my application, I have:
main thread -> Gui Thread that implements front end.
subyacent thread -> Makes some long work (no gui at all, plain code), implemented in a Custom class named MyWorker.
In some places of subyacent thread's code, I need to emit a signal that have to be catched by the main thread. In a previous version, I did it this way:
Main thread's code
MyWorker's code
This code is running in a test environment from some weeks ago while I finish some details of the GUI, and I didn't see anything wrong. Yesterday searching in the web I casually noticed that QObject::moveToThread can't move objects with a parent
[1]]https:[//forum.qt.io/topic/40653/qobject-movetothread-cannot-move-objects-with-a-parent][1]
so I'm afraid that my code may have problems that I have not yet detected.
I modified this part of code, and now it is:
New code for main Thread
New code for MyWorker
So, now, I don't really know how to connect this signal to a slot that lives in main thread, cause if I'm not wrong, there is no parent/children relation since I changed thread affinity.
Could someone please tell me if there is a way to do this, and guide me towards documentation about this problem?
Many thanks in advance.
I'm very new in Python, so I'm not an expert at all, and probably it's a begginer's question.
In my application, I have:
main thread -> Gui Thread that implements front end.
subyacent thread -> Makes some long work (no gui at all, plain code), implemented in a Custom class named MyWorker.
In some places of subyacent thread's code, I need to emit a signal that have to be catched by the main thread. In a previous version, I did it this way:
Main thread's code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#Slot for clicked button def Slot_Start_Calculation ( self ): self .MyLongThread = QThread() MyLongWorker = MyWorker( self , Qty) MyLongWorker.moveToThread( self .MyLongThread) self .MyLongThread.started.connect(MyLongWorker.Run_LongWorker) MyLongWorker.WorkerFinished.connect( self .MyLongThread.quit) MyLongWorker.WorkerFinished.connect( self .MyLongWorker.deleteLater) MyLongThread.finished.connect( self .MyLongThread.deleteLater) MyLongThread.start() def Slot_Worker_Sent_A_Signal ( self , Result): print ( "Worker sent this result: " + str (Result)) . . . |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class MyWorker(QObject): WorkerFinished = Signal() CustomSignal = Signal( int ) def __init__( self , parent, Quantity) super ().__init__(parent) self_parent = parent self .Quantity = Quantity print ( "Myworker's constructor" ) def __del__( self ): print ( "Myworker's destructor" ) def Run_LongWorker ( self ): self .CustomSignal.connect( self .parent.Slot_Worker_Sent_A_Signal) . . . self .CustomSignal.emit(MyPartialResult) . . . self .WorkerFinished.emit() |
[1]]https:[//forum.qt.io/topic/40653/qobject-movetothread-cannot-move-objects-with-a-parent][1]
so I'm afraid that my code may have problems that I have not yet detected.
I modified this part of code, and now it is:
New code for main Thread
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#New slot for clicked button. def Slot_Start_Calculation ( self ): MyLongThread = QThread() MyLongWorker = MyWorker(Qty) MyLongWorker.moveToThread(MyLongThread) MyLongThread.started.connect(MyLongWorker.Run_LongWorker) MyLongWorker.WorkerFinished.connect(MyLongThread.quit) MyLongWorker.WorkerFinished.connect(MyLongWorker.deleteLater) MyLongThread.finished.connect(MyLongThread.deleteLater) MyLongThread.start() def Slot_Worker_Sent_A_Signal ( self , Result): print ( "Worker sent this result: " + str (Result)) . . . |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class MyWorker(QObject): WorkerFinished = Signal() CustomSignal = Signal( int ) def __init__( self , Quantity) super ().__init__() self .Quantity = Quantity print ( "Myworker's constructor" ) def __del__( self ): print ( "Myworker's destructor" ) def Run_LongWorker ( self ): self .CustomSignal.connect( ??? .Slot_Worker_Sends_A_Signal) . . . self .CustomSignal.emit(MyPartialResult) . . . self .WorkerFinished.emit() |
Could someone please tell me if there is a way to do this, and guide me towards documentation about this problem?
Many thanks in advance.