Important: Please read the Qt Code of Conduct -

QThread - Error - QThread: Destroyed while thread is still running

  • Hi,
    I am new to PySide2. I am just trying to launch a sample application and start a thread as the application starts and want to stop the thread as the application closes. When I am closing the application I am getting the following error:
    QThread: Destroyed while the thread is still running.
    The is a python file that I converted from sample_ui.ui


    import time
    import sys
    import sample_ui
    from PySide2 import QtWidgets
    from PySide2 import QtCore
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            main_window_ui = sample_ui.Ui_MainWindow()
            self.custom_thread = CustomThread()
        def closeEvent(self, event):
            QtWidgets.QMainWindow.closeEvent(self, event)
    class CustomThread(QtCore.QThread):
        def __init__(self):
            super(CustomThread, self).__init__()
        def run(self):
            while self.isRunning():
               print("Thread is Running")
        def stop(self):
            print("Thread Stopped")
    if __name__ == '__main__':
        app = QtWidgets.QApplication()
        main_window = MainWindow()
    Thread is Running
    Thread is Running
    Thread is Running
    Thread Stopped
    QThread: Destroyed while thread is still running

  • Lifetime Qt Champion

    You have to wait until the thread is really finished, see the documentation

Log in to reply