Important: Please read the Qt Code of Conduct -

[SOLVED] modal QDialog::setVisibible(false) calls the Destructor

  • Hello !

    If I hide a modal dialog, this calls its destructor. This not what I want.
    Must I configure something to avoid it?

  • Lifetime Qt Champion

    Are you sure its not a case of running out of scope ?

    void func() {
    MyDia A;

    } << A dies here, even if hidden.

  • Hello @mrjj !
    Indeed the Destructior comes from by subclassing of the QDialog and some mechansim.
    I have tested it with a simple QDialog now. The main problem stays:

    QDialog d;
    d.exec()               //  in the dialog call hide() under some conditions;
    // resume code

    The exec() returns if I hide(). This I dont want. The hide() should be only a visual effect.

    (My program logic seem a little bit strange, but I develop on a tablet a full-screen app with a large dialog-stack. The lower dialogs I want to hide, so that the user seen only the top-dialog in the windows taskbar.)

  • Lifetime Qt Champion


    Have you tried to give the lower dialog the main window as parent ?
    Like in
    QDialog d(mainwin);
    As that normally will make sure only mainwin has entry in taskbar.

    Hmm not sure you can ask Exec to keep running if you hide the dialog.
    Did you try with Show instead ?
    So you want your dialog to stay alive, but not visible before you press button or how does it become visible again?

  • @mrjj
    Thank you for the tip. With correct parent all work right.
    (My idea before was Hide it -> Exec other Dialog -> after return Show it again.)
    But this is all not needed now.

    Nevertheless only hiding a dialog would be usefull sometimes. Seem that Qt doesnt support it.
    Only Show instead of exec would complicate the program flow and I think the showed dialog wouldnt be enabled if I call it from a modal dialog.

  • Lifetime Qt Champion


    Yes, using Show does make program flow more complicated but it can be managed ok easy using signals when when you hide / show dialogs. But I agree with Modal Dialogs, its get messy fast :)

  • @Seem that Qt doesnt support it.

    Modal dialogs have their own event loop which starts when you call exec.
    When dialog is closed by user exec has to return otherwise you stuck - user can't exit exec, cause dialog is hidden.

    But all it does - stops event loop and hides dialog, nothing else.
    This does not delete dialog, so it actually does what you want.
    And you can exec() again.

    MyDialog dialog;

    while ( dialog.exec() == QDialog::Accepted )

    In your case object is deleted cause it goes out of scope, If you do not want it to be deleted allocate it on the stack.
    But normally you do not want to keep an instance of the modal dialog unless its initialization is really slow.

    Choice between modelless and modal dialog is not made based on the fact you want to keep it hidden.
    Difference is in the ability to interact with other windows/dialogs when your dialog is shown.
    And choice of modality defines which functions you use exec() or show()/hide().

Log in to reply