Qt World Summit: Register Today!

How to properly override QOpenGLWidget::resizeEvent ?

  • It seems that QOpenGLWidget::paintEvent and QOpenGLWidget::resizeEvent do unwanted glClear.

    I don't want to clear the screen before every paintGL call, but Qt does it automatically, so i've overrided QOpenGLWidget::paintEvent like this:

    void GlWidget::paintEvent(QPaintEvent* event)

    Seems that it works fine.

    Then i've tried to override QOpenGLWidget::resizeEvent (I don't need to call glViewPort 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 after paintGL) 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:

    1. Although rendering (without resizing) works fine, do I miss some calls in the overrided paintEvent?
    2. Which calls should I add into the overrided resizeEvent, which are called in the base class resizeEvent?

  • Moderators

    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 to glClear() before painting by calling QOpenGLWidget::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 modifying QOpenGLWidget (specifically QOpenGLWidgetPrivate::recreateFbo()). Is that glClear() 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.

Log in to reply