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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on 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?

    JonBJ 1 Reply Last reply
    0
    • CJhaC CJha

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      • kkoehneK Offline
        kkoehneK Offline
        kkoehne
        Moderators
        wrote on 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

        • Login

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