Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QThread - Error - QThread: Destroyed while thread is still running
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 5.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pratik Tayshete
    wrote on last edited by
    #1

    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 sample_ui.py is a python file that I converted from sample_ui.ui

    Code:

    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()
            main_window_ui.setupUi(self)
            self.custom_thread = CustomThread()
            self.custom_thread.start()
    
        def closeEvent(self, event):
            self.custom_thread.stop()
            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")
               time.sleep(1)
        def stop(self):
            print("Thread Stopped")
            self.quit()
    
    
    
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication()
        main_window = MainWindow()
        main_window.show()
        sys.exit(app.exec_())
    
    
    **Output:**
    Thread is Running
    Thread is Running
    Thread is Running
    Thread Stopped
    QThread: Destroyed while thread is still running
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved