QDialog: Reasons for avoiding exec()?
-
Hi, according to Qt's documentation for
int QDialog::exec()
function:Note: Avoid using this function; instead, use open(). Unlike exec(), open() is asynchronous, and does not spin an additional event loop. This prevents a series of dangerous bugs from happening (e.g. deleting the dialog's parent while the dialog is open via exec()). When using open() you can connect to the finished() signal of QDialog to be notified when the dialog is closed.
I am using multiple threads in my application for calculations. I have done a few tests with signals-slots in different ways, including with signals from threads connected to slots in my main window while my
QDialog
is opened withexec()
. All signal and slot connections seem to be working without any issues. The entire application is working without any issue.I searched the internet and I am unable to find a good reason as to why Qt's documentation asks us to avoid using
int QDialog::exec()
function, apart from the single example of deleting dialog's parent that it mentions. I can easily take care of making sure that the dialog's parent is not deleted (or rather the dialog is closed before the parent is deleted) while the dialog is open viaexec()
.I want to use
int QDialog::exec()
in my application as it makes it much easier to get inputs from the user. Are there any other major reasons to avoid this function? -
The best summary is probably still https://www.qt.io/blog/2010/02/23/unpredictable-exec - wow, 12 years ago! I'm getting old ;)
From my own experience, QDialog::exec() is usually fine, especially for modal dialogs. But the exact interactions depends a lot on all the other things you're doing in your app ...