Keep a dialog open until reject()

  • I'm opening a dialog with:

    if(dialog.exec() == QDialog::Accepted)
    // do stuff based on returned values

          if (retval == 1)
       if (retval == 2)
              // do something else with values from dialog


    Is there a way to keep the dialog open until the reject() signal is passed by the dialog's cancel button?

  • Lifetime Qt Champion


    Do you mean something like using a while loop until the user cancels the dialog ?

  • Would a do/while loop be the way? Would looping the exec repaint the dialog each time the loop cycled?

    The dialog is a form, the form's buttons offer several choices for manipulating the data the user enters. The user may well want to do more than one thing with the data on the form. Now, they have to re-open the dialog after each action. That's fine if they only wanted to do that one thing, but that's not always the case

  • Lifetime Qt Champion

    Then are you sure that returning from the dialog is the right way to interact with your users ?

    You could use an "Apply" button that will trigger a slot that parses your dialog contents thus it would avoid needing to close it each time or use a loop.

  • Sometimes you simply do not need apply. you want action to be executed if everything is right, but want user to fix wrong input first
    Often you can achieve this with validators.
    But you do not have to.
    Subclassing is usually a key to everything when you dial with good designed code in C++.

    In your case you could override and do all the checks in
    void QDialog::done(int r)


    • hides dialog
    • set results
    • emits some signals

    So in your version can call it only if user either rejected or everything is right.

    @void MyDialog::done(int r)
    bool canClose = true;

    if ( QDialog::Accepted )
          ... do whatever you need 
           ... if can't close yet , for example
           .... user accepted but data he entered was invalid set
          canClose = false;
    if (canClose)


  • Thank you for the thoughts. Someone else suggested that instead of using a dialog for a form, I should consider using a new window and just keep it open until I'm done with it. Lost to think about here, my only QT code has been single-window only.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.