Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: How to know when a QMdiSubWindow is closed?

Tags:

qt

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?

like image 392
Pherrymason Avatar asked Jan 11 '12 11:01

Pherrymason


1 Answers

Yes, there is another way : you can install an event-filter on the QMdiSubWindow you create :

MdiSubWindowEventFilter * p_mdiSubWindowEventFilter;

...

QMdiSubWindow * subWindow = mdiArea->addSubWindow(pEmbeddedWidget);
subWindow->installEventFilter(p_mdiSubWindowEventFilter);
subWindow->setAttribute(Qt::WA_DeleteOnClose, true); // not mandatory, depends if you manage subWindows lifetime

with

bool MdiSubWindowEventFilter::eventFilter(QObject * obj, QEvent * e)
{
    switch (e->type())
    {
        case QEvent::Close:
        {
            QMdiSubWindow * subWindow = dynamic_cast<QMdiSubWindow*>(obj);
            Q_ASSERT (subWindow != NULL);

            //
            // do what you want here
            //

            break;
        }
        default:
            qt_noop();
    }
    return QObject::eventFilter(obj, e);
}
like image 106
azf Avatar answered Nov 15 '22 10:11

azf