20

I'm having problem with QThreads in python. I want to change background color of label. But My application crash while starting. "QThread: Destroyed while thread is still running"

   class MainWindow(QMainWindow):
      def __init__(self):
          QMainWindow.__init__(self)
          self.ui = Ui_MainWindow()
          self.ui.setupUi(self)

          statusTh = statusThread(self)
          self.connect(statusTh, SIGNAL('setStatus'), self.st, Qt.QueuedConnection)
          statusTh.start()

      def st(self):
          if self.status == 'ON':
              self.ui.label.setStyleSheet('background-color:green')
          else:
              self.ui.label.setStyleSheet('background-color:red')

  class statusThread(QThread):
      def __init__(self, mw):
          super(statusThread, self).__init__()

      def run(self):
          while True:
              time.sleep(1)
              self.emit(SIGNAL('setStatus'))

  if __name__ == "__main__":
      app = QApplication(sys.argv)
      main_window = MainWindow()
      main_window.show()
      sys.exit(app.exec_())
Sergey Kostin
  • 309
  • 1
  • 2
  • 5

1 Answers1

37

You're not storing a reference to the thread after it's been created, which means that it will be garbage collected (ie. destroyed) some time after the program leaves MainWindows __init__. You need to store it at least as long as the thread is running, for example use self.statusTh:

self.statusTh = statusThread(self)
self.connect(self.statusTh, SIGNAL('setStatus'), self.st, Qt.QueuedConnection)
self.statusTh.start()
rainer
  • 6,199
  • 2
  • 21
  • 36
  • Sorry, I am novice in Python. I can't understand. How can I need to store the reference? – Sergey Kostin Mar 29 '13 at 12:24
  • 9
    Just as i wrote in my answer. By assigning the instance of `statusThread(self)` to a local variable (ie. without `self.` in front of it), it will be garbage collected when the local variable goes out of scope (which it does when `__init__` of `MainWindow` is finished). If you store the reference in a member variable of the `MainWindow` class (ie. with `self.` in front of it), it will not go out of scope when `__init__` is done, resulting in the thread object not being garbage collected. – rainer Mar 29 '13 at 14:47
  • I would give 10 upvotes! Nice answer - saved me a lot of time and grief! I think you should move the comment text to the answer as well. – Eugene Sajine Mar 07 '14 at 19:28
  • Excuse me, but if you `dont want` to store the references - is there any method to create a new reference to object(thread)? I have a lot of methods which are need to work concurrently. – Victor Polevoy Sep 08 '14 at 17:42