开发者

QLabel setText not displaying text immediately before running other method

开发者 https://www.devze.com 2023-01-31 15:37 出处:网络
I have a basic label that is supposed to indicate to the user that the program is searching directories for several seconds. So it goes like...

I have a basic label that is supposed to indicate to the user that the program is searching directories for several seconds. So it goes like...

self.label.setText(QString("Searching..."))
# method to search directories goes here
self.label.setText(QString("Search Complete"))

My problem is that the label never displays "Searching...". The execution always seems to jump straight to run the method to scan directories, and then the label 开发者_如何学运维text is set to "Search Complete" after the method which scans directories has finished.

I'd be grateful if someone could please explain why this is happening or suggest a better way to resolve the problem.

many thanks


Your "method to search directories" is blocking the GUI hence QLabel is not able to update the text. You can make your search routine asynchronous or go the easy way and force QLabel to update itself:

self.label.setText(QString("Searching..."))
self.label.repaint()
# method to search directories goes here
self.label.setText(QString("Search Complete"))


Add include:

#include <qapplication.h>

Let Qt process events:

self.label.setText(QString("Searching..."))
qApp->processEvents();

Note: repaint() was not necessarie.


In PyQt5, you don't need to use QString :

self.label.setText("Searching...")
self.label.repaint()
self.label.setText("Search Complete")
0

精彩评论

暂无评论...
验证码 换一张
取 消