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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With