Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to grab the Framebuffer in QOpenGLWidget class.



  • HI,
    I am trying to grab the framebuffer in QOpenGLWidget Class. But for some reason I am getting the Empty QImage. Below is my code. I am using FrameBufferObject. Anything wrong? Because I want the pixels to render in my OpenGL context.

    saveBackBuffer()
    {
    _bSavingBackBuffer = true;

     QString fileName("C:\\Users\\ey617e\\Desktop\\yourFile.png");
     QFile file(fileName);
     file.open(QIODevice::WriteOnly);
    
     glReadBuffer(GL_BACK);
      
      makeCurrent();
      QOpenGLFramebufferObjectFormat format;
      format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
      QOpenGLFramebufferObject * fbo = new 
      QOpenGLFramebufferObject(_scrWidth, _scrHeight, format);
      fbo->bind();
      paintGL();
    
     _savedBackBuffer = fbo->toImage();
     _savedBackBuffer.save(file.fileName(), "PNG");
      fbo->release();
    

    }

    paintGL()
    {
    QOpenGLPaintDevice fboPaintDev(_scrWidth, _scrHeight);
    QPainter painter(&fboPaintDev);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    painter.beginNativePainting();
    
    drawDisplayLists(_underIllusDisplayLists);
    drawDisplayLists(_illusDisplayLists);
    
    painter.endNativePainting();
    
    painter.end();
    

    }


  • Lifetime Qt Champion

    Hi,

    Taking out your image saving code, are you getting anything rendered on your widget ?



  • Yes It is getting rendered.


  • Lifetime Qt Champion

    @BPrasad said in How to grab the Framebuffer in QOpenGLWidget class.:

    QOpenGLFramebufferObject

    Are you sure that it is valid ?


Log in to reply