1

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?

AQuick
  • 168
  • 1
  • 1
  • 8
  • I am having this issue, in a large app. My min. example doesn't show the issue. My only option is to gut my code (in a copy) until the problem disappears or I have a min example. – MathCrackExchange Sep 01 '19 at 18:12
  • I got mine to go away by chopping out some code. I think it had to do with when the thread was created relative to the window. I'm getting some runtime warnings though regarding threads. I will fix those too. – MathCrackExchange Sep 01 '19 at 19:40

2 Answers2

1

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:

handle
  • 5,859
  • 3
  • 54
  • 82
1

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')
AQuick
  • 168
  • 1
  • 1
  • 8