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

Closing QMdiSubWindow From Within Widget



  • Anybody know the proper way to close a QMdiSubWindow programmatically from within the QWidget it contains? For instance, I have a requirement to put a "Close" button on a form, which when clicked should close the QMdiSubWindow that contains the form. The best solution I've come up with is to set the parent of the form to the QMdiSubWindow and then connect the button's clicked() signal to the close slot of the parentWidget. Such as:

    @
    QMdiSubWindow* sw = new QMdiSubWindow( mdiArea );
    sw->setWidget( new MyForm( sw ) );
    sw->setAttribute( Qt::WA_DeleteOnClose );
    mdiArea->addSubWindow( sw );
    sw->show();
    @

    Then within MyForm constructor I would connect the signal:

    @
    connect( closeButton, SIGNAL( clicked() ),
    parentWidget(), SLOT( close() ) );
    @

    This approach only works if MyForm's parentWidget is the QMdiSubWindow. I can't help thinking there must be a better way. Seems like there should be some kind of property or attribute on QMdiSubWindow like "close me when widget closes" but I haven't found it. Anyone have any ideas?



  • Looks ok to me. The way you do it is actually one of the two that the Qt documentation mentions.

    I'm not aware of an easier way to achieve this.



  • Thanks for the feedback, I kept thinking I was missing something. I couldn't find anything in the Qt docs about this kind of thing. What documentation mentions this way and what is the other way?



  • It's in the docs of "QMdiArea::addSubWindow() ":http://doc.qt.nokia.com/4.7/qmdiarea.html#addSubWindow


Log in to reply