2

I have a QWidget that has a very cpu intensive paint event handler. But it hardly needs updating, although it is moved on screen. I know how to implement a double buffer paint mechanism, but as all QWidgets already have a double buffering system, I was curious to see if it is possible to use it on purpose, something like this:

 void ParametersWidget::paintEvent(QPaintEvent *)
 {
   if(isnt_changed) {
      bypassUpdate();
   }
   else {
     drawStuff();
   }
 }

Any help would be appreciated.

arashka
  • 1,226
  • 3
  • 17
  • 30

1 Answers1

0

You can't just ignore the paint event as your widget might for instance have been covered by another window, and when this window is moved your widget needs to be repainted. One possible optimization is to paint only the dirty region. It can be obtained by calling region() of the QPaintEvent. No reason to repaint your whole widget if only a part of it needs to be repainted.

Without knowing exactly what your widget contains it's hard to give any more specific optimization hints. If the contents of the widget requires expensive database queries or a lot of calculations, you should of course make sure to perform those only when needed, save the result, and use the result during repaints.

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
  • and how exactly does this relate to my question, about double-buffering? – arashka Apr 30 '15 at 17:44
  • 1
    You mentioned you had a very CPU intensive paint handler and I thought you wanted to try to improve the performance. When I read you question I got the impression that you thought double buffering would help you achieve that, but double buffering has nothing to do with performance (only flickering). If you are not interested in how to improve the performance, please ignore my answer. For double buffering you need to do nothing. Qt does that for you automatically since Qt 4.0. – Daniel Hedberg Apr 30 '15 at 17:57
  • 1
    Look, let me explain. As you said it too, Qt does double-buffering automatically. Which means that it stores a snapshot of my widget as a pixmap in RAM, and renderes that instead of calling my widget's paint function when it decides. Now consider that it has received an update signal and it calls the paint function, BUT my widget knows that it doesn't need to be updated. Obviously, I can keep a pixmap of the last paint and rerender it, but I wanted to see whether there is a way to use the built-in buffered pixmap, instead of keeping it manually in an extra buffer. – arashka Apr 30 '15 at 19:48
  • 2
    Hmm, I don't know enough of the Qt internals to give a qualified answer to this, but at the time of the paint event one could suspect that Qt has already cleared its back buffer and associated it with the painter so that you can paint on it. The back buffer should be painter->device() I guess. – Daniel Hedberg May 02 '15 at 12:17