Search code examples
qtfocusqpainter

QPainter redraw on window gaining/losing focus


I am learning Qt and am trying to paint a simple display for my program with QPainter.

I draw static elements (frames etc.) once and only update dynamic elements afterwards.

Everything works fine, except for when the window loses focus. As soon as that happens, the whole area is cleared (dynamic elements keeps being painted as before).

Is it possible to prevent this behaviour? If not, how do I determine if the window have lost focus?


Solution

  • While I did not find why the screen was repainted, the focus can be triggered by using

    eventFilter(QObject *, QEvent *event) {
    if (event->type() == QEvent::ActivationChange){}
    }
    

    and paint function can be called from here. Although a slight delay must be added, as the trigger usually fires before the window looses focus (hence still clearing the repaint).