QOpenGLWidget::grabFrameBuffer() always ignores alpha channel?



  • I have an application using QOpenGLWidget, and I want to be able to export images by grabbing the frame buffer, where the background "clear color" may optionally contain transparencies.
    But, it seems that no matter what I do, the QImage returned by grabFrameBuffer() always returns false for QImage::hasAlpha().

    I am currently setting the default surface format like so:

    auto qsf = QSurfaceFormat::defaultFormat();
    if (!qsf.hasAlpha()) {
    	qsf.setAlphaBufferSize(8);
    	QSurfaceFormat::setDefaultFormat(qsf);
    }
    

    I even do this before instantiating QApplication, based on the Note: section in the documentation here

    Note: Calling QSurfaceFormat::setDefaultFormat() before constructing the QApplication instance is mandatory on some platforms (for example, macOS) when an OpenGL core profile context is requested. This is to ensure that resource sharing between contexts stays functional as all internal contexts are created using the correct version and profile."""
    

    Is there something else I'm missing that's needed to get the alpha from this function, or is it a bug in QOpenGLWidget?

    It seems like the description of QOpenGLWidget::grabFrameBuffer is a lie when it says "Renders and returns a 32-bit RGB image of the framebuffer."
    Since it appears to only return 24 bits and throw away alpha channel.

    I don't understand why the incredibly useful withAlpha argument from deprecated QGLWidget:
    QGLWidget::grabFrameBuffer(bool withAlpha = false)
    was removed in QOpenGLWidget which replaced it?



  • I had to peek at the source and found that it has been a bug, fixed in this commit: https://codereview.qt-project.org/c/qt/qtbase/+/235928

    As far as I understand this fixed was applied to Qt 5.12 and up.

    Unfortunately I'm on Ubuntu 18.04 which is still on 5.9.5.
    Is there any chance of this fix being backported to earlier versions?


  • Moderators

    @thehans said in QOpenGLWidget::grabFrameBuffer() always ignores alpha channel?:

    Unfortunately I'm on Ubuntu 18.04 which is still on 5.9.5.
    Is there any chance of this fix being backported to earlier versions?

    Qt 5.9 is now in "very strict" update mode, which means in only receives security fixes. It no longer receives bug fixes.

    Your options are:

    I think the first option is easier.