问题
I have a simple pyqt gui which creates a qthread to open a file and read some information. I want to update the statusbar of my gui. Normally, this would be my function call to update a message on the statusbar:
class gui1(QtGui.QMainWindow):
def __init__(self, parent=None):
super(gui1, self).__init__(parent)
self.statusBar().showMessage("hello world")
...
# create thread here
self.mythread = WorkThread(text) # create instance and pass some text
self.mythread.finished.connect(self.threadDone) # signal emitted on finish of thread
self.mythread.start() # begin thread
However, the call to update the status within the thread does't work. How can I update the statusbar for my gui from within the qthread?
class WorkThread(QtCore.QThread):
def __init__(self,text):
self.text = text
QtCore.QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
self.ui.statusBar().showMessage(status) # WRONG SELF
return # must return, so that Qthread finished signal is emitted
回答1:
You must never attempt to update the GUI from outside the GUI thread. Instead, add a custom signal to the worker thread and connect it to a slot in the GUI:
class WorkThread(QtCore.QThread):
statusMessage = QtCore.pyqtSignal(object)
...
def run(self):
self.statusMessage.emit(self.text)
class gui1(QtGui.QMainWindow):
def __init__(self, parent=None):
super(gui1, self).__init__(parent)
self.mythread = WorkThread(text)
...
self.mythread.statusMessage.connect(self.handleStatusMessage)
@QtCore.pyqtSlot(object)
def handleStatusMessage(self, message):
self.ui.statusBar().showMessage(message)
来源:https://stackoverflow.com/questions/22237254/qthread-updating-ui-statusbar