Take the 2-minute tour ×

Is there any way I can get notified when a user closes a QMdiSubWindow? I cannot find any signal in QMdiArea nor in QMdiSubWindow that suggests I can.

I think my only chance is by subclassing QMdiSubWindow and overriding the close event, but is there any other way?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

I don't think there is any other way than as you describe (overriding the close event) to do precisely what you're asking.

There might be other ways of achieving what you want without doing that depending on why you want to know when its closed. Other options could be the use of the destroyed signal, checking QApplication::focusWidget(), or perhaps having the parent inspect its children.

Edit in response to comment:

Signals and slots are disconnected automatically upon destruction of QObjects, and I would suggest looking at using QSharedPointers or QScopedPointers to handle your QObjects' lifespans instead. By applying these techniques, you shouldn't need a signal from a closed window.

share|improve this answer
I need to clean some connections between signals/slots and delete some objects. –  Raúl Ferràs Jan 11 '12 at 11:45
add comment

Here is what I ended coding.


#include <QMdiSubWindow>
#include <QCloseEvent>
#include <QDebug>

class MyQMdiSubWindow : public QMdiSubWindow

    void closed( const QString );

    void closeEvent( QCloseEvent * closeEvent )
        emit closed( this->objectName() );

Note that for my problem I need a way to identify which subwindow user is closing, and objectName does the work for me.

share|improve this answer
add comment

You can create QWidget based class like:

class CloseWatcher : public QWidget

  QString m_name;

  void disposing( QString name );

  CloseWatcher( QWidget * p )
    : QWidget( p )
    , m_name( p->objectName() )

    emit disposing( m_name );

and just use it:

// anywhere in code
QMdiSubWindow * wnd = getSomeWnd();
CloseWatcher * watcher = new CloseWatcher( wnd );

connect( watcher, SIGNAL( disposing( QString ) ), reveiver, SLOT( onDispose( QString ) ) );
share|improve this answer
add comment

Your Answer


