QLabel setText does not print text immediately before running another method

I have a base label that should tell the user that the program is looking for directories in a few seconds. So this is 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 shows “Search ...”. Execution is always like starting a directory scanning method, and then the label text is set to “Search Complete” after the method that scans directories has completed.

I would appreciate it if someone could explain why this is happening, or suggest a better way to solve the problem.

many thanks

+8
qt pyqt
source share
2 answers

Your "directory search method" blocks the GUI, so QLabel cannot update the text. You can make your search procedure asynchronous or make a simple 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")) 
+16
source share

Add:

 #include <qapplication.h> 

Let Qt handle events:

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

Note: repaint () was optional.

0
source share

Source: https://habr.com/ru/post/650383/


All Articles