Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QGLWidget sometimes clears content before paintEvent
QAndreas last edited by
I have a problem concerning the QGLWidget. My rendering is very expensive (involves millions of particles), so I only want to render a new frame when it's necessary. Unfortunately, when Qt sends me a paintEvent and I don't render a new frame and call swapBuffers, sometimes the widget becomes white. For example, this occurs if I minimize and maximize my application. Qt seems to fill the widget area with white before invoking the paint event. Is there any way to prevent this? If I don't render a new frame and swap it in paintEvent, the content of the widget should just stay the same. I tried setAutoFillBackground(false), but it seems to have no effect. I also tried setAttribute(Qt::WA_OpaquePaintEvent) without success.
A possible workaround is to only call swapBuffers without rendering a new frame in paintEvent. This works, but it seems a bit dangerous to me because I read that the back buffer becomes undefined after swapBuffers so in theory it should not work to swap without rendering a new frame first under all platforms.
I reproduced my problem in the simple hellogl Qt demo. I added in the GLWidget constructor:
Since auto swapping is set to false, I added swapBuffers at the end of paintGL. Also I modified paintGL so that it only renders and swaps once, so only at the first paintEvent a new frame is rendered. When the program is minimized and maximized again the widget is white.
Note that this does not only occur on minimize and maximize (that wouldn't be a problem), but minimize and maximize is the only case where I can reproduce it properly. It is also an example why I don't want to render a new frame everytime a paint event occurs: When the widget is minimized, a paint event occurs (why?). When it is maximized, two paint events occur! (why two?)
I use Qt 5.1.1 and Windows 7.
Edit: The problem does not seem to occur under Ubuntu. Interesting.