void MatchModel::btnTrainClicked(){ UI2MatchParam(); ui.setWidget->setVisible(false);//隱藏 ui.statusWidget->setVisible(true);//顯示 //qApp->processEvents();//加上這條語句后達到預期目的,解決問題 bool flag = match->CreateShapeModel(matchParam); QMessageBox message(QMessageBox::NoIcon, "狀態", flag ? " 建模完成 " : " 建模失敗! "); message.exec(); ui.setWidget->setVisible(true);//顯示 ui.statusWidget->setVisible(false);//隱藏 }
源程序代碼所要實現的功能,當主操作界面點擊按鈕“訓練”時,執行該槽函數,最終的效果為,將原本界面其中一個顯示的QWidget隱藏,而把原本隱藏的一個QWidget顯示在界面上,如下圖所示:
點擊“訓練”按鈕前:
點擊“訓練”按鈕但還未訓練完成:
點擊“訓練”按鈕並且訓練完成后:
而未加函數qApp->processEvents();前,在訓練過程中並未顯示第二個圖中的效果,即原本界面其中一個顯示的QWidget隱藏,而把原本隱藏的一個QWidget顯示在界面上。等到訓練完成后調用了message.exec();才會顯示隱藏對應的QWidget。說明程序執行以下兩條語句時,並不能直接實時更新界面,
ui.setWidget->setVisible(false);//隱藏
ui.statusWidget->setVisible(true);//顯示
而在調用
message.exec();
便可刷新一次界面。
問題解決:https://jingyan.baidu.com/article/d5a880eb6d5f7f13f147ccff.html
在對控件進行操作后,然接着 qApp->processEvents(),這句代碼便能及時刷新界面,至於程序中執行message.exec()后便能書信界面,那是由於message.exec()函數本身就有調用到
qApp->processEvents()。
對於
qApp->processEvents()的英文解釋
Processes all pending events for the calling thread according
to the specified flags until there are no more events to process.
You can call this function occasionally when your program is busy
performing a long operation(e.g.copying a file).In event you are
running a local loop which calls this function continuously, without
an event loop, the DeferredDelete events will not be processed.This
can affect the behaviour of widgets, e.g.QToolTip, that rely on DeferredDelete
events to function properly.An alternative would be to call sendPostedEvents()
from within that local loop.