【问题标题】:PyQt4 QThread / moveToThread not working depending on context?PyQt4 QThread / moveToThread 不根据上下文工作?
【发布时间】:2012-12-18 19:48:55
【问题描述】:

我试图理解为什么下面的代码会立即退出,但是如果我在主上下文中创建线程而不是在第二个对象中创建线程?

from PyQt4 import QtCore
import time
import sys

class SomeObject(QtCore.QObject):

    finished = QtCore.pyqtSignal()

    def longRunning(self):
        count = 0
        while count < 5:
            time.sleep(1)
            print "Increasing"
            count += 1
        self.finished.emit()

class SecondObject(QtCore.QObject):
    def __init__(self, app):
        QtCore.QObject.__init__(self)
        objThread = QtCore.QThread()
        obj = SomeObject()
        obj.moveToThread(objThread)
        obj.finished.connect(objThread.quit)
        objThread.started.connect(obj.longRunning)
        objThread.finished.connect(app.exit)
        objThread.start()

def usingMoveToThread():
    app = QtCore.QCoreApplication([])
    SecondObject(app)
    sys.exit(app.exec_())

if __name__ == "__main__":
    usingMoveToThread()

提前感谢您的帮助!

【问题讨论】:

    标签: python multithreading pyqt pyqt4


    【解决方案1】:

    我发现了问题。显然您需要保留新线程的引用,否则应用程序将退出......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-10
      • 2021-07-17
      • 2013-10-15
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 2011-08-05
      相关资源
      最近更新 更多