I just wrote some QThread based code that executes a big calculation. To visualize the progress I need to open a QProgressDialog. The dialog is application modal (using open()) since I do not want to allow modifications of the main window during calculation. The thread emits various signals that allow state machine based communication between GUI and thread.
Two of the signals emitted by the thread's worker object are "Progress" and "Finished". If "Progress" is emitted I am updating the QProgressDialog using setValue(). If "Finished" is emitted the dialog is destroyed.
The following happens at the end of calculation:
- "Progress" event (100%) is emitted
- "Finished" is emitted directly after
- setValue(100) is called due to "Progress" event
- Because the dialog is modal, setValue() calls processEvents()
- processEvents() delivers the "Finished" event
- The "Finished" event causes the Dialog to be destroyed in the middle of setValue() which causes a crash
QProgressDialog breaks my architecture by calling processEvents() in setValue(). Also my coding conventions forbid usage of any nested event loops (like in exec() etc.).
I have two questions:
Why does a modal dialog require a nested event loop? From my undestanding blocking the parent windows' input seem not to require this.
Is it possible to use QProgressDialog in a modal way but without a nested event loop?