hoodakaushal
hoodakaushal

Reputation: 1293

PyQt Not updating label

I've created a simple application to transfer files using Paramiko and SFTP. I also wanted to have a statusbar (a QLabel) to inform the user when I'm downloading/uploading stuff. So, my function to upload looks something like this:

def upload(self):
        self.statusLabel.setText('Uploading')
        local = str(self.uploadLineEdit.text())
        filename = os.path.basename(local)
        remote = "/home/" + self.userName + "/testdata/" + filename
        self.ftp.put(local, remote)
        self.uploadedFileName = filename
        self.statusLabel.setText('Upload Finished')

Notice that before starting the upload I change the statusbar to uploading, and when upload is done, I change it to upload finished.

However, what actually happens is that the "Uploading" message is never displayed on the label - it just goes straight to "Upload Finished". I suspect this is because the changes happen only after the function returns.

How do I get the label to change at the start of the upload process?

Upvotes: 5

Views: 4581

Answers (1)

Andrzej Pronobis
Andrzej Pronobis

Reputation: 36086

You might need to force processing of events after changing the label text. Try adding:

QApplication.processEvents()

after setting the label text.

Please note that for a reason not known to me, PyQt and PySide both tend to have problems with processEvents, which sometimes needs to be executed multiple times to take effect. So, if it does not work after adding a single processEvents(), try adding it twice, or even multiple times.

Upvotes: 11

Related Questions