| 49 | } |
| 50 | |
| 51 | bool eventFilter(QObject* watched, QEvent* event) override |
| 52 | { |
| 53 | // Saving geometry on a nonspontaneous hide event is the only choice that is both reliable and safe: |
| 54 | // * the close event is received only when a dialog is dismissed via |
| 55 | // its standard window close button and not when it is accepted or rejected; |
| 56 | // * the QDialog::finished() signal is unavailable for non-dialog widgets (less general) and is not emitted |
| 57 | // if the dialog is explicitly hidden or destroyed while visible rather than finished normally; |
| 58 | // * Qt documentation does not directly promise to emit the QObject::destroyed() signal from ~QWidget() |
| 59 | // as opposed to the too-late ~QObject(). Also Qt developers do not guarantee that the state |
| 60 | // of the widget being destroyed can be safely accessed in a slot connected to this signal. |
| 61 | // Some parts of the widget, e.g. its layout, are destroyed before the destroyed() signal is emitted. |
| 62 | // A downside of handling the hide event is that when a widget is hidden, then shown again, its geometry |
| 63 | // is saved multiple times redundantly. Fortunately, a dialog is unlikely to be shown again |
| 64 | // after being hidden. If the redundant saving becomes a bottleneck for some widget, the visibility |
| 65 | // of which changes often, a custom saving of geometry can be implemented in that widget's destructor. |
| 66 | // See also the discussion of when to save geometry at https://codereview.qt-project.org/c/qt/qtbase/+/498797 |
| 67 | if (event->type() == QEvent::Hide && !event->spontaneous()) { |
| 68 | const auto& widget = this->widget(); |
| 69 | Q_ASSERT(watched == &widget); |
| 70 | configGroup().writeEntry(entryName(), widget.saveGeometry()); |
| 71 | } |
| 72 | return false; // do not filter any events out |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | static QString entryName() |
no test coverage detected