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. QOpenGLWindow - paintGL is never called
Qt 6.11 is out! See what's new in the release blog

QOpenGLWindow - paintGL is never called

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 1.7k 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.
  • G Offline
    G Offline
    glararan
    wrote on last edited by
    #1

    Hello,

    I'm learning with new Qt classes like VAO, VBO, OpenGLWindow..

    I took example and little bit rewrite it. Because example wasn't using paintGL but QPaintDevice.

    Currently I have following code:

    #include "glwindow.h"
    
    #include <QCoreApplication>
    #include <QDebug>
    
    GLWindow::GLWindow()
    : QOpenGLWindow(QOpenGLWindow::NoPartialUpdate)
    {
    }
    
    GLWindow::~GLWindow()
    {
    }
    
    void GLWindow::initializeGL()
    {
        initializeOpenGLFunctions();
    
        connect(this, &GLWindow::frameSwapped, this, &GLWindow::renderNow); // use this or last commented out section in renderNow()?
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    
        initialize();
    
        renderNow();
    }
    
    void GLWindow::paintGL()
    {
        qDebug() << "c";
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    
        render();
    
        painted = true;
    }
    
    void GLWindow::renderNow()
    {
        if(!isExposed())
            return;
    
        /*if(!painted) // useless to continue when paint wasnt done yet
            return;
    
        painted = false;*/
    
        QOpenGLContext::currentContext()->makeCurrent(this);
    
        QOpenGLWindow::update(); // <---- this not calling paintGL at all :((
    
        QOpenGLContext::currentContext()->swapBuffers(this);
    
        qDebug() << "draw";
    
        // use connect or this? what is faster?
        /*if(draw && !pendingUpdate)
        {
            pendingUpdate = false;
    
            QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
        }*/
    }
    
    void GLWindow::resizeGL(int width, int height)
    {
        const qreal pixelRatio = devicePixelRatio();
    
        glViewport(0, 0, width * pixelRatio, height * pixelRatio);
    }
    
    bool GLWindow::event(QEvent* event)
    {
        switch(event->type())
        {
            case QEvent::UpdateRequest:
                {
                    pendingUpdate = false;
    
                    renderNow();
    
                    return true;
                }
    
            default:
                return QWindow::event(event);
        }
    }
    
    void GLWindow::exposeEvent(QExposeEvent* event)
    {
        Q_UNUSED(event);
    
        if(isExposed())
            renderNow();
    }
    
    void GLWindow::initialize() // virtual
    {
    }
    
    void GLWindow::render() // virtual
    {
    }
    

    This should be class which will be inherited.

    What's the problem? I get from qDebug only "draw" not "paintGL" or "c". PaintGL should be called from line "QOpenGLWindow::update(); // <---- this not calling paintGL at all :((" or I had to missunderstand documentations.

    Also last question, I did some comments to code, is better to use connect to repaint or QCoreApplication::postEvent?

    Thanks

    1 Reply Last reply
    0
    • G Offline
      G Offline
      glararan
      wrote on last edited by glararan
      #2

      Ok I figured it out, there is broken code in example.

      bool GLWindow::event(QEvent* event)
      {
          switch(event->type())
          {
              case QEvent::UpdateRequest:
                  {
                      pendingUpdate = false;
      
                      renderNow();
      
                      return true;
                  }
      
              default:
                  return QWindow::event(event);
          }
      }
      

      Should be

      bool GLWindow::event(QEvent* event)
      {
          if(event->type() == QEvent::UpdateRequest)
          {
                 pendingUpdate = false;
      
                 renderNow();
          }
      
          return QWindow::event(event);
      }
      

      So there is only question which remains, what's better to use as update? PostEvent or connect?

      Thanks

      EDIT: next question, why is QOpenGLWindow limited to 30 frames per second? How to make 60fps?

      EDIT 2: fps solved with setSwapInterval(0)
      EDIT 3: I guess to FPS counter that QCoreApplication::PostEvent is 5 times faster than connect, but still give me your advice

      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