2010-12-22 50 views
8

我有一個基本標籤,用於向用戶指示程序正在搜索目錄幾秒鐘。所以它就像...QLabel setText在運行其他方法之前不立即顯示文本

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

我的問題是,標籤從不顯示「正在搜索...」。執行似乎總是直接跳轉到運行該方法來掃描目錄,然後在掃描目錄的方法結束之後標籤文本被設置爲「搜索完成」。

如果有人能解釋爲什麼會發生這種情況,或者提出解決問題的更好方法,我將不勝感激。

千恩萬謝

回答

16

你「的方法來搜索目錄」擋住了GUI,因此QLabel無法更新文本。你可以讓你的搜索程序異步還是走簡單的方法,迫使QLabel自我更新:

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

太好了。非常感謝您的回覆。 – Kim 2010-12-22 16:46:06

0

添加包括:

#include <qapplication.h> 

讓Qt的過程中的事件:

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

注意: repaint()不是必需的。

相關問題