销毁GUI时在PySide中杀死QThread

2024-09-26 18:11:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个滑动条,它根据从中心点拖动的距离来“扭曲”值。我想用踏板做翘曲来正确地做。这是我关于实际计时的线索:

class WarpThread(QtCore.QThread):
    errorHappened = False

    # This will be a semaphore used to wake up the warper thead when the slider
    # is being manipulated. It starts at 0. Upon sliderPressed, it is set to +1.
    # when the slider is released, it is set -1 again. The thread then will
    # wait on the semiphore till it is > 0. When it is, it steps up our value,
    # sleeps and then waits again
    doWarp = None

    updateWarp = Signal()
    def __init__(self, parent = None):
        super(WarpThread, self).__init__(parent)
        self.exiting = False
        self.doWarp = QtCore.QSemaphore(0)

    def run(self):
        while True:
            self.doWarp.acquire()
            if self.exiting:
                break
            self.updateWarp.emit()
            self.doWarp.release()
            QtCore.QThread.msleep(50)

    # === External function calls ===
    def startWarping(self):
        self.doWarp.release() # Allow the thread to start looping

    def stopWarping(self):
        self.doWarp.acquire()

    def stopThread(self):
        self.exiting = True
        self.doWarp.release()
        self.wait()

因此,当接收到信号sliderPressed时,扭曲滑块只调用startWarping,当接收到sliderReleased时调用stopWarping。一切都很好。计划是在有滑块的东西的stopThread内部调用__del__。好吧,当我不做任何事情就关闭GUI时,它会很好地关闭。当我在使用滑块后退出时,我得到一个QThread: Destroyed while thread is still running错误。我试着让线程不是带有滑块的线程的父线程(认为Qt可能会通过继承树破坏它),但这似乎没有起到任何作用。所以我想我不明白线程什么时候被破坏了。在

Qt是否会销毁不在x-edout GUI的整个父子树中的对象? 为什么停止使用线程的小部件的__del__中的线程似乎不起作用? 为什么只有用过东西才有问题?在

如果你有有用的链接,请留下他们的评论。我很难找到答案,部分原因是我缺乏有效搜索的声音

编辑

另外,我知道有人建议将线程全局化以确保它们不会被杀死,但是每个滑块都有一个线程。。。也许这就是需要改变的设计要点。。。待续。在


Tags: thetoselfreleaseisdefit线程

热门问题