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

Dialog does not close correctly



  • Hello,
    I have a dialog box with a custom "Cancel" button. The "Cancel" button clicked event is connected to the QDialog close slot.
    When I click this "Cancel" button, all the widgets of the dialog box disapeared but the dialog itself does not close. When I click the cross at the top-right corner of the dialog, everything is close correctly. I don't know what I'm doing wrong. Here is a part of my code :

        // Open the dialog
        CMyDialog *child = new CMyDialog ();
        child->setAttribute(Qt::WA_DeleteOnClose);
        m_mdiArea->addSubWindow(child);
        child->show();
    
        // Dialog's Constructor 
        CMyDialog ::CMyDialog (QWidget *parent) :
        QDialog(parent),
        ui(new Ui::CMyDialog )
        {
          ui->setupUi(this);
          connect(ui->m_btCancel, SIGNAL(clicked()), this, SLOT(close()));
          ...
        }
    
    
    

    Any help is welcome. Thanks.



  • @OlivierDz
    Because you are putting this dialog/window/widget into an QMdiSubWindow you must not close the dialog itself, you must perform such operations on the MDI window which owns/shows it, That is what the the "cross" is doing. You can access the parent MDI subwindow via child->parentWidget() if you need to.



  • @OlivierDz
    Because you are putting this dialog/window/widget into an QMdiSubWindow you must not close the dialog itself, you must perform such operations on the MDI window which owns/shows it, That is what the the "cross" is doing. You can access the parent MDI subwindow via child->parentWidget() if you need to.



  • @JonB said in Dialog does not close correctly:

    Thanks, it works now.

    I created a custom slot for the Cancel button in my CMyDialog class.
    I this slot a put this->parentWidget()->close().
    This solved the problem.


Log in to reply