QStackedWidget pages and closeEvent()/eventFilter()
-
I have a mature product with uncommented code written by people who like spaghetti. I have come across code I do not understand. However, I must be careful not to change it if I do not understand the behaviour/change in behaviour.
The app has a main window, with a
QStackedWidget
which holds all its "sub-pages" (QWidget
s), selectable via a toolbar.Various of these pages wish to save their state (or whatever action) on main window close. (Please do not suggest on destruction; there are issues with that from Python, and I wish to perform the action on close.)
I am used to using a
QWidget::closeEvent()
override for this. However, left to its devices, although the main window gets a close event theQStackedWidget
pages do not (tested empirically).The current code for detecting in these sub-pages is instead:
def eventFilter(self, watched, event): if watched == self.window() and event.type() == QEvent.Close: saveState(__name__, self.grid.header()) return self.parent().eventFilter(watched, event)
-
The bit I don't get is the use of
self.parent().eventFilter()
.self.parent()
is theQMainWindow
. Why is this widget deferring to the parent'seventFilter()
, when the usual pattern for an override issuper().eventFilter()
wheresuper()
is the base class in Python? It seems to me: I will have many of these pages'eventFilter()
getting called on main close, so they will call the main window'seventFilter()
that many times, which seems odd? -
I suppose a different approach would be for the main window/stacked widget to explicitly call
QWidget.close()
on each & every widget in the stacked widget, then I would get thecloseEvent()
on them. However that would require changing the calling code somewhere to do so, whereas (for right or for wrong) at the moment theeventFilter()
of the sub-pages is getting called automatically without me having to do anything, so I'd rather like to stick with it...?
-