Alex Rivera | Logout

pyqt: receive signal when widget becomes visible/hidden

Asked 2012-06-07T06:48:07.913
25

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.

Edit
Report

1 Answer

37

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).

answered 2012-06-07T07:11:48.000

Your Answer