Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

can't close the Qdialog



  • hi, everyone.
    I have a class A which is a QDialog. in its constructor, it must read something from a file and if the file is not opened, the QDialog must be closed. I used

    this->close();
    

    but it doesn't work. what should I do???
    thank you very much


  • Moderators

    In the constructor the dialog is not even shown yet, so there's nothing to close.

    Usually with dialogs you have a code like this:

    SomeDialog dialog;
    dialog.exec();  //exec calls show() internally
    

    so if you want to prevent it from showing you can do something like this:

    SomeDialog dialog;
    if (dialog.isInitialized())
        dialog.exec();
    

    where isInitialized() would return true or false depending on what the constructor did.


Log in to reply