AQuick
AQuick

Reputation: 176

QThread signal emitting twice

Created a Qthread to handle a version check for my application. The Thread works perfectly, except that it is emitting the finished signal twice.

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

How do I prevent the signal from being emitted twice?

Upvotes: 1

Views: 770

Answers (2)

AQuick
AQuick

Reputation: 176

Thanks to @handle and the documents you provided, I was able to find my answer.

I needed to create the signal in the thread class. My working code is below.

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

Upvotes: 1

handle
handle

Reputation: 6339

Your class myThread inherits from QtCore.QThread which already has the finished signal, so I assume the problem is your (superfluous) line self.finished.emit().

These look like they might help:

On SO:

Docs:

Docs for PySide Qt Bindings:

Upvotes: 1

Related Questions