Search code examples
qtqthreadqt-signals

How to get known if QThread exited properly or was terminated?


I have found in Qt 4.8 there was terminated signal as you can see here: http://doc.qt.io/archives/qt-4.8/qthread.html#terminated

But now in Qt 5.8 there is no such a thing. http://doc.qt.io/archives/qt-5.8/qthread.html

It seems finished signal is emmited if thread finished and even if thread is terminated. But is there option how to get known if QThread exited properly or was terminated?


Solution

  • I used following pattern proposed by @Scheff

    class MyQThread(QThread):
        def __init__(self):
            self.__terminated = None
            super().__init__()
    
        def wasTerminated(self):
            if self.__terminated is None:
                return True
            return False
    
        def run(self):
            # Add at end of run
            self.__terminated = False
    

    And following in caller:

    class Worker(QObject):
        def __init__(self):
            super().__init__()
            self.thread = MyQThread()
            self.thread.finished.connect(self.finishedSlot)
    
        def finishedSlot(self):
            if self.thread.wasTerminated():
                print("Thread was killed before finished")
            else:
                print("Thread results are ok")