How to use QTimer inside QThread which uses QWaitCondition? (pyside)

和自甴很熟 提交于 2019-12-04 15:11:21

To still process events in a long running task (aka a continuous loop) you need to call QCoreApplication::processEvents().

This will essentially go through all of the queued up slots for your thread.

Calling this function is also necessary for signals (if they are a QueuedConnection signal/slot connection) to make it out of the current thread and into another one.


For PySides, you will need to call PySide.QtCore.QCoreApplication.processEvents()

Marek R

Your method loop completely occupies thread. It doesn't return control to event loop. Timer sends its events to event loop which doesn't gain control.
IMO your wile loop is faulty.

One way to fix it is add QApplication.processEvents() in loop (bad approach).

I think you want something else, here are my corrections:

def loop(self):
    self.timer = QtCore.QTimer()
    self.timer.setSingleShot(False)
    self.timer.timeout.connect(self.do_stuff_timer)
    self.timer.start(1000)

def stop(self):
    self.timer.stop()

this will invoke do_stuff_timer every second until you will call stop.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!