在进入单独的函数之前强制Qt GUI进行更新 [英] Forcing the Qt GUI to update before entering a separate function
问题描述
这似乎应该是自动的,但显然不是。我有以下代码:
This seems like it should be automatic, but apparently it's not. I have the following code:
ui.my_label->setText("Test 1...");
ui.my_label->adjustSize();
processThatTakesAbout30SecondsToFinish(files[0].toStdString());
ui.my_label->setText("Finished.");
ui.my_label->adjustSize();
发生的是我永远不会看到 Test1 ...,
,因为GUI似乎挂起,直到下面的函数完成,我eventaully只看到完成。
What is happening is that I never see "Test1...",
as the GUI seems to hang until the following function completes, and I eventaully only see "Finished.".
如何在进入该功能之前确认GUI正在更新?
谢谢。
How can I make sure the GUI is updating before it enters that function?
Thanks.
推荐答案
您可以在输入代码之前处理事件队列
You shuld be able to process the event queue before entering your code if you;
#include <QApplication>
,当您想刷新GUI时,请呼叫
and, when you want to refresh your GUI, call;
qApp->processEvents();
请注意,让长时间运行的进程调用该函数可能是个好主意,让您的GUI感觉更加敏感。
Note that it may be a good idea to let your long running process call that function now and then, to make your GUI feel more responsive.
这篇关于在进入单独的函数之前强制Qt GUI进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!