Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDialog: Reasons for avoiding exec()?

QDialog: Reasons for avoiding exec()?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qdialogexec
3 Posts 3 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CJha
    wrote on 23 Feb 2022, 09:22 last edited by
    #1

    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 with exec(). 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 via exec().

    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?

    J 1 Reply Last reply 23 Feb 2022, 12:34
    0
    • C CJha
      23 Feb 2022, 09:22

      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 with exec(). 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 via exec().

      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?

      J Offline
      J Offline
      JonB
      wrote on 23 Feb 2022, 12:34 last edited by
      #2

      @CJha
      I know it says that, but I and loads of examples have always used QDialog::exec() and not encountered the dire warnings it mentions....

      1 Reply Last reply
      2
      • K Offline
        K Offline
        kkoehne
        Moderators
        wrote on 23 Feb 2022, 12:35 last edited by
        #3

        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 ...

        Director R&D, The Qt Company

        1 Reply Last reply
        4

        1/3

        23 Feb 2022, 09:22

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved