python - QT Timers not calling function -
i'm using pyqt python3.
my qtimers not calling function they're told connect to. isactive() returning true, , interval() working correctly. code below (works standalone) demonstrates problem: thread started, timer_func() function never called. of code boilerplate pyqt. far can tell, i'm using in accordance docs. it's in thread event loop. ideas?
import sys pyqt5 import qtcore, qtwidgets class thread(qtcore.qthread): def __init__(self): qtcore.qthread.__init__(self) def run(self): thread_func() def thread_func(): print("thread works") timer = qtcore.qtimer() timer.timeout.connect(timer_func) timer.start(1000) print(timer.remainingtime()) print(timer.isactive()) def timer_func(): print("timer works") app = qtwidgets.qapplication(sys.argv) thread_instance = thread() thread_instance.start() thread_instance.exec_() sys.exit(app.exec_())
you're calling thread_func withn run method of thread, means timer create in function lives in thread's event loop. start threads event loop, must call it's exec_() method from within it's run method, not main thrad. in example here, app.exec_() never gets executed. make work, move exec_ call thread's run.
an additional problem timer gets destroyed when thread_func finishes. keep alive, must keep reference somewhere.
import sys pyqt5 import qtcore, qtwidgets class thread(qtcore.qthread): def __init__(self): qtcore.qthread.__init__(self) def run(self): thread_func() self.exec_() timers = [] def thread_func(): print("thread works") timer = qtcore.qtimer() timer.timeout.connect(timer_func) timer.start(1000) print(timer.remainingtime()) print(timer.isactive()) timers.append(timer) def timer_func(): print("timer works") app = qtwidgets.qapplication(sys.argv) thread_instance = thread() thread_instance.start() sys.exit(app.exec_())
Comments
Post a Comment