简体   繁体   English

PyQt4 QThread / moveToThread不能根据上下文工作?

[英]PyQt4 QThread / moveToThread not working depending on context?

I'm trying to understand why the following code immediately exits, but works if I create the thread in the main context and not in a second object? 我试图理解为什么以下代码立即退出,但是如果我在主上下文而不是第二个对象中创建线程,则可以正常工作吗?

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()

Thanks in Advance for any help! 在此先感谢您的帮助!

I've found the problem. 我发现了问题。 Apparently you need to hold on to the new Thread's reference otherwise the Application will just quit... 显然,您需要保留新线程的引用,否则应用程序将退出...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM