Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyqt: receive signal when widget becomes visible/hidden

Tags:

signals

qt

pyqt

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.

like image 578
Rolle Avatar asked Jun 07 '12 06:06

Rolle


1 Answers

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

like image 120
Ammar Avatar answered Oct 06 '22 22:10

Ammar