From Capacious Bird, 3 Weeks ago, written in Plain Text.
  1. import sys
  2. import time
  3. from PyQt5.QtCore import *
  4. from PyQt5.QtWidgets import *
  5. from PyQt5.QtGui import *
  6.  
  7. class A(QWidget):
  8.  
  9.     A_signal=pyqtSignal()
  10.  
  11.     def __init__(self):
  12.         super().__init__()
  13.                
  14.         self.Bobj=B()
  15.         self.Cobj=C(self.Bobj)
  16.    
  17.         self.threadB=QThread()
  18.         self.threadC=QThread()
  19.    
  20.         self.Bobj.moveToThread(self.threadB)
  21.         self.Cobj.moveToThread(self.threadC)
  22.  
  23.         self.A_signal.connect(self.Cobj.C_function)        
  24.         #self.Bobj.B_signal.connect(self.Bobj.B_function)       #B_function runs in threadB
  25.         #self.Bobj.B_signal.connect(self.A1_function)       #B_function runs in parent thread because A1_function runs in parent thread
  26.         #self.Cobj.C_signal.connect(self.Bobj.B_function)       #B_function runs in threadB
  27.         #self.Cobj.C_signal.connect(self.A2_function)           #B_function runs in parent thread because A2_function runs in parent thread
  28.         self.Cobj.C_signal.connect(self.Cobj.C1_function)   #B_function runs in threadC -> I wanted this
  29.        
  30.         self.threadB.start()
  31.         self.threadC.start()
  32.  
  33.         print("A:"+str(int(QThread.currentThreadId())))
  34.  
  35.         self.A_signal.emit()
  36.  
  37.     def A1_function(self,l):
  38.         self.Bobj.B_function(l)
  39.        
  40.     def A2_function(self,l):
  41.         self.Bobj.B_function(l)
  42.  
  43.  
  44. class B(QObject):
  45.     B_signal=pyqtSignal(list)
  46.    
  47.     def B_function(self,l):
  48.         print("B:"+str(int(QThread.currentThreadId())))
  49.  
  50. class C(QObject):
  51.     C_signal=pyqtSignal(list)
  52.  
  53.     def __init__(self,b):
  54.         super().__init__()
  55.         self.Bobj=b
  56.         #self.C_signal.connect(self.Bobj.B_function)         #B_function runs in parent thread
  57.         #self.C_signal.connect(self.C1_function)                #B_function runs in parent thread
  58.  
  59.    
  60.     def C_function(self):
  61.         l=[]
  62.         i=1
  63.         while i<100:
  64.             print("C:"+str(int(QThread.currentThreadId())))
  65.             l.append(i)
  66.             self.C_signal.emit(l)
  67.             i=i+1
  68.             time.sleep(1)
  69.    
  70.     def C1_function(self,l):
  71.         self.Bobj.B_function(l)
  72.  
  73. def main():
  74.         app=QApplication(sys.argv)
  75.         ex=A()
  76.         ex.show()
  77.         sys.exit(app.exec_())
  78.  
  79. if __name__ == "__main__":
  80.     main()
  81.  
captcha