Why QApplication::processEvents(QEventLoop::AllEvents, 20);?
-
Hi,
in some older code QCoreApplication::processEvents is used before a loop starts and always at the end of the loop. Inside the loop content of a derived version of a QTextEdit is rendered (the content could be a lot).
I am not sure what the benefit of the using of processEvents is?
Some collegue figgured out that the combination of processevents and the closing of a dialog could lead to a crash and i am thinking about if i could just delete the processEvents ... ? Aren't the events processed anyway?
Thank you!
-
Rule of thumb: Do not use QCoreApplication::processEvents() at all. As you see it can lead to crashes because e.g. signals & slots are executed during this call. This means another signal is executed while you're still in a slot and this signal can change the state of the object you're currently working on (and also destroy it).
-
@Christian-Ehrlicher Thanks! So what is the intendet purpose of it? Do I understand
The event loop is started with a call to exec(). Long-running operations can call processEvents() to keep the application responsive.
right, that for example if I save for example a big file, the GUI would still response to interaction and not "blocked"?
-
@firen
You could callprocessEvents()
if you are looping saving a block at a time to file. That would keep the UI responsive, but then you must be prepared to handle that your UI code may go off doing something while you are still in a slot saving a file.A better way for saving a large file would probably be to do it in a thread.
As @Christian-Ehrlicher says the rule of thumb is to try to avoid
processEvents()
, but it's not forbidden.