QThread信号发出两次

时间:2018-09-22 14:23:04

标签: python pyqt

创建了一个Qthread来处理我的应用程序的版本检查。线程工作正常,只是它发出两次完成的信号。

class myThread(QtCore.QThread):

    def run(self):
        print("Im running.")
        self.finished.emit()

    def stop(self):
        self.terminate()

class myApp(QtWidgets.QMainWindow, Ui_App):

    def __init__(self, parent=None):
        super(myApp, self).__init__()

        self.myThread = myThread()
        self.myThread.start()

        self.myThread.finished.connect(self.allDone)

    def allDone(self):
        print('Thread is done')

如何防止信号两次发射?

2 个答案:

答案 0 :(得分:0)

您的类myThread继承自已经具有QtCore.QThread信号的finished,所以我认为问题出在您的(多余的)行self.finished.emit()

这些看起来可能会有所帮助:

关于SO:

文档:

PySide Qt绑定文档:

答案 1 :(得分:0)

由于@handle和您提供的文档,我得以找到答案。

我需要在线程类中创建信号。我的工作代码如下。

class myThread(QtCore.QThread):

    signalStatus = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(UpdateThread, self).__init__(parent)

    def run(self):
        print("Im running.")
        self.signalStatus.emit()

    def stop(self):
        self.terminate()

class myApp(QtWidgets.QMainWindow, Ui_App):

    def __init__(self, parent=None):
        super(myApp, self).__init__()

        self.myThread = myThread()
        self.myThread.start()

        self.myThread.finished.connect(self.allDone)

    @QtCore.pyqtSlot()
    def allDone(self):
        print('Thread is done')