Search code examples
c++windowsqtpaintqscrollarea

Qt - avoid white background when resizing (set immediately a background color)


The question is in bold at the end so please read this as a whole.

I have a QAbstractScrollArea widget that I manually and completely render in the OnPaint() event.

In its constructor I set

setAttribute( Qt::WA_OpaquePaintEvent, true );
setAttribute( Qt::WA_NoSystemBackground, true );
setStyleSheet( "QWidget { background-color: rgb(0,0,77); }" );

and the paint event looks like this:

void MyArea::paintEvent (QPaintEvent *event) {
  QPainter view(viewport());
  view.fillRect(rect(), backgroundBrush);

  renderedPixmap = heavyAndSlowRenderingOnAPixmap();

  view.drawPixmap(viewRect, renderedPixmap, pixmapRect);
}

as you can see there's a "slow" function involved to render stuff into a pixmap.

The problem is: when I resize the window, I can see for a moment a white flickering in the area not yet redrawn

enter image description here

I know I can't avoid the white region until the viewport has been redrawn, but I'd like to draw that white region with the background color immediately.

So here's the question: can I display a background color immediately before the heavy-pixmap rendering?

I can't seem to find a way to achieve this.. it seems all graphics operations are buffered and then immediately bit-blitted to the screen together. I'm using Windows 8.1 x64 and Qt5. Any way to immediately draw the background color and then proceed with the rest of the rendering?


Solution

  • The best solution would be to move the expensive rendering out of the paintEvent(), potentially into another thread. You'll want to cache the rendered pixmap anyway. Update that pixmap when really needed and then call update() to trigger a repaint.