python - Custom signals and multithreading -


i've been trying make code work, still can't see flaw is. i'm trying emit signal new thread, main receives signal , executes function.

if try within same thread, works fine - code, thread created, signal never connected.

from pyqt4.qtcore import *  pyqt4.qtgui import * pyqt4 import qtcore  class workthread(qtcore.qthread):     def __init__(self):         qtcore.qthread.__init__(self)      def run(self):         print("from thread")         self.emit(qtcore.signal("trying"))         return  class foo(qobject):                 def handle_trigger(self):         print ("trigger signal received")      def new_thread(self):         self.get_thread = workthread()         self.connect(self.get_thread, qtcore.signal("trying"), self.handle_trigger)         self.get_thread.start()   = foo() a.new_thread() 

edited based on comments.

there 1 main problem code. you're not starting qt application, there no main event loop running. must add following lines:

app = qapplication(sys.argv) app.exec_() 

also, should use new-style qt signals/slots if possible, or if stick old-style, know qt signal should in form of c function if want work pyside. change work pyside, qtcore.signal("trying()"), not qtcore.signal("trying"). see comments (specifically mine , @ekhumoro's comments) details.

here's working version of code (using old-style signals/slots), tried change least amount possible see small changes. had use pyside instead, should work pyqt well:

from pyside.qtcore import *  pyside.qtgui import * pyside import qtcore import sys  class workthread(qtcore.qthread):     def __init__(self):         qtcore.qthread.__init__(self)      def run(self):         print("from thread")         self.emit(qtcore.signal("trying()"))  class foo(qobject):     def handle_trigger(self):         print ("trigger signal received")         self.get_thread.quit()         self.get_thread.wait()         qapplication.quit()      def new_thread(self):         self.get_thread = workthread()         self.connect(self.get_thread, qtcore.signal("trying()"), self.handle_trigger)         self.get_thread.start()  = foo() a.new_thread()  app = qapplication(sys.argv) app.exec_() 

and here's version using new signal/slot style (see @three_pineapples comment). recommended way implement signals/slots in pyqt/pyside.

from pyside.qtcore import *  pyside.qtgui import * pyside import qtcore import sys  class workthread(qtcore.qthread):     ranthread = qtcore.signal()     # pyqt, use qtcore.pyqtsignal() instead      def __init__(self):         qtcore.qthread.__init__(self)      def run(self):         print("from thread")         self.ranthread.emit()  class foo(qobject):     def handle_trigger(self):         print ("trigger signal received")         self.get_thread.quit()         self.get_thread.wait()         qapplication.quit()      def new_thread(self):         self.get_thread = workthread()         self.get_thread.ranthread.connect(self.handle_trigger)         self.get_thread.start()  = foo() a.new_thread()  app = qapplication(sys.argv) app.exec_() 

Comments