Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to properly override QOpenGLWidget::resizeEvent ?

How to properly override QOpenGLWidget::resizeEvent ?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mr.indieperson
    wrote on last edited by mr.indieperson
    #1

    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)
    {
       makeCurrent();
       paintGL();
       paintWithQPainter();
    }
    

    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.

    0_1540673462501_24c214b8-56fd-4eea-b172-eef8fdd968d3-image.png

    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?
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?

      M 1 Reply Last reply
      1
      • Chris KawaC Chris Kawa

        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?

        M Offline
        M Offline
        mr.indieperson
        wrote on last edited by mr.indieperson
        #3

        @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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved