How to properly override QOpenGLWidget::resizeEvent ?
-
It seems that
QOpenGLWidget::paintEvent
andQOpenGLWidget::resizeEvent
do unwantedglClear
.I don't want to clear the screen before every
paintGL
call, but Qt does it automatically, so i've overridedQOpenGLWidget::paintEvent
like this:void GlWidget::paintEvent(QPaintEvent* event) { makeCurrent(); paintGL(); paintWithQPainter(); }
Seems that it works fine.
Then i've tried to override
QOpenGLWidget::resizeEvent
(I don't need to callglViewPort
right here):void GlWidget::resizeEvent(QResizeEvent* event) { // QWidget::resizeEvent(event); // i've also tried this, doesn't help resizeGL(event->size().width(), event->size().height()); }
When I resize the window, something goes wrong, for example: if I decrease window's height, the drawn text (it is drawn using
QPainter
afterpaintGL
) has less height and the letters look like compressed, also antialiasing doesn't work properly. You can see it at the picture, look at the text, don't pay attention to colored area.So I have 2 questions:
- Although rendering (without resizing) works fine, do I miss some calls in the overrided
paintEvent
? - Which calls should I add into the overrided
resizeEvent
, which are called in the base classresizeEvent
?
- Although rendering (without resizing) works fine, do I miss some calls in the overrided
-
QOpenGLWidget
uses a framebuffer object and holds and modifies some internal state in these event handlers. I would advise against overriding them. Even if it works now (it clearly doesn't) it will break unexpectedly on future updates.Instead of overriding
paintEvent()
you can prevent a call toglClear()
before painting by callingQOpenGLWidget::setUpdateBehavior(QOpenGLWidget::PartialUpdate)
.As for resizing - it recreates the internal FBO and sets a bunch of internal state. You can't safely override that without messing the widget's state (see the source here). Unfortunately I don't see any way to disable the call to
glClear()
there without modifyingQOpenGLWidget
(specificallyQOpenGLWidgetPrivate::recreateFbo()
). Is thatglClear()
call really that problematic in your case? -
QOpenGLWidget
uses a framebuffer object and holds and modifies some internal state in these event handlers. I would advise against overriding them. Even if it works now (it clearly doesn't) it will break unexpectedly on future updates.Instead of overriding
paintEvent()
you can prevent a call toglClear()
before painting by callingQOpenGLWidget::setUpdateBehavior(QOpenGLWidget::PartialUpdate)
.As for resizing - it recreates the internal FBO and sets a bunch of internal state. You can't safely override that without messing the widget's state (see the source here). Unfortunately I don't see any way to disable the call to
glClear()
there without modifyingQOpenGLWidget
(specificallyQOpenGLWidgetPrivate::recreateFbo()
). Is thatglClear()
call really that problematic in your case?@Chris-Kawa Thank you,
QOpenGLWidget::setUpdateBehavior(QOpenGLWidget::PartialUpdate)
is a good answer. Missed that option.As for resizing - now I don't want to override it just to prevent glClear(), as it's impossible, anyway the resizing is a pretty rare event comparing to repainting.