Fixing a Frozen Status Bar in PyQt4

In one of my recent apps a long-running web request blocked the UI, so I wanted a status bar message to show what the background task was doing.

Multithreading approach

I had solved a similar problem before with a worker thread:

1
2
3
4
5
6
7
8
9
10
11
12
class SetLabelText(QThread):
def __init__(self, label, message, parent=None):
super(SetLabelText, self).__init__(parent)
self.label = label
self.message = message

def __del__(self):
self.exiting = True
self.wait()

def run(self):
self.label.setText(u"%s" % self.message)

And inside the time-consuming section:

1
2
self.m1 = SetLabelText(self.status_label, u"Reminder")
self.m1.start()

A simpler option

Threads work, but when you update the status message multiple times in the same method you end up handling extra complexity. I found a simpler approach: update the label directly and immediately flush the event loop:

1
2
self.status_label.setText(u"Loading configuration… please wait…")
QCoreApplication.processEvents()

Reference:

How to use Qt multithreading correctly