25

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
  • 359
  • 1
  • 3
  • 5
  • 2
    A Qt-style solution, assign main window as `statusTh`'s parent, namely, `super(statusThread, self).__init__(mw)`. – nymk Mar 29 '13 at 12:23
  • Just for information, this is basically the same thing: in this case, the parent will keep the reference to the thread instance and therefore it will not be garbage collected. – rainer Mar 29 '13 at 14:48
  • Can anyone please explain , what does that Qt.QueuedConnection means in the above question ? – Natesh bhat Aug 13 '17 at 02:56
  • 1
    so basically it's just a typo? – greendino Nov 04 '20 at 23:37
  • 1
    The `super().__init__()` call that's in the example above is evidently a typo, as it does nothing with the `mw`. – Mike C Dec 02 '20 at 02:14

3 Answers3

42

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,769
  • 3
  • 23
  • 37
  • 1
    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. – VP. Sep 08 '14 at 17:42
1

I know it's quite necroposting but this may be useful. In the main section of your script, a first level customized widget need to be stored in variable, not only created. For example I have a custom widget class called MainWindow that create a QThread. My main is like that:

from myPackage import MainWindow
if __name__ == "__main__":
    app = QApplication([])    
    widget=MainWindow()    
    sys.exit(app.exec())

if I avoid the widged = definition and only call for MainWindow(), my script will crash with QThread: Destroyed while thread is still running

Lamaresh
  • 11
  • 1
0

Add statusTh.wait() after statusTh.start():

...
statusTh.start()
statusTh.wait()
...
Drumsman
  • 677
  • 5
  • 5