pyqt: receive signal when widget becomes visible/hidden

pyqt, qt, signals

Solution

One solution is, you can override `QWidget::showEvent()` and `QWidget::hideEvent()` function in your widget (documentation). And then `emit` you custom `signal` and catch in a `slot` in the respective object. For example..

void MyWidget::hideEvent(QHideEvent *)
{
    // 'false' means hidden..
    emit widgetVisibilityChanged(false);
}

void MyWidget::showEvent(QShowEvent *)
{
    // 'true' means visible..
    emit widgetVisibilityChanged(true);
}

Now if you cannot override your widget, you can also receive above events in its parent widget using `QObject::installEventFilter ( QObject * filterObj )` and `QObject::eventFilter ( QObject * watched, QEvent * event )` combination (documentation and example).

Problem

I have noticed there is no signal/event for when a QWidget becomes visible/invisible. Is there anything else I can hook to get roughly the same thing (except polling isVisible())? I want to turn of some data fetching if the widget that displays the data is not visible.

Original source