QTimer和QThread奇怪的行为

时间:2014-12-12 17:49:34

标签: python pyside qthread qtimer

from PySide.QtGui import *
from PySide.QtCore import *
import sys
from time import sleep

class MyWorkerThread(QThread):
    def __init__(self, parent=None):
        super(MyWorkerThread, self).__init__(parent)

    def run(self):
        timer1 = QTimer()
        timer1.singleShot(1000, self.alarm_goes1)
        print "Timer 1 Start"
        timer1.start()

    def alarm_goes1(self):
        print "goes off 1"


class MainFrame(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.thread = MyWorkerThread(self)
        print "Thread Start"
        self.thread.start()

        timer2 = QTimer()
        timer2.singleShot(1000, self.alarm_goes2)
        print "Timer 2 Start"
        timer2.start()

    def alarm_goes2(self):
        print 'goes off 2'

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = MainFrame()
    main.show()
    sys.exit(app.exec_())

输出:

Thread Start
Timer 2 Start
Timer 1 Start
goes off 2

请解释为什么alarm_goes1不会执行。我怎么能做到呢?


编辑:

def run(self):
    timer1 = QTimer()
    timer1.singleShot(2000, self.alarm_goes1)
    print "Timer 1 Start"
    timer1.start()
    self.exec_()
    self._alive = True
    while self._alive:
        print 'this part will not execute'
        self.sleep(1)

使用self.exec_()这两个计时器都可以正常工作,但是while循环中的部分不会出现问题。如果没有self.exec_(),只有一个定时器可以工作,但是while循环可以工作。

我希望定时器和while循环一起工作。

我尝试在while循环中移动self.exec_(),但它只会运行一次。

1 个答案:

答案 0 :(得分:1)

计时器发布QTimerEvent,但线程中没有运行事件循环来处理它。

所以你需要做这样的事情:

    def run(self):
        timer1 = QTimer()
        timer1.singleShot(1000, self.alarm_goes1)
        print "Timer 1 Start"
        timer1.start()
        # start the clock
        clock = QTimer()
        clock.start(1000)
        clock.timeout.connect(self.tick)
        # start the thread's event loop
        self.exec_()

    def tick(self):
        print 'tick'

并且将它添加到MainFrame类:

可能是明智的
    def closeEvent(self, event):
        # exit the thread's event loop
        self.thread.quit()