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

Transparent QOpenGLWidget in Dialog



  • When I add a QOpenGLWidget in a frameless dialog, and open the dialog on a qwidget, I can see the dialog is transparent as shown in the screenshot. What I am expecting is for the red to have alpha but it shouldn't see the button in the main widget.

    #include "Widget.h"
    
    #include <QHBoxLayout>
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    #include <QPushButton>
    
    class MyOpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
    {
    public:
        explicit MyOpenGLWidget(QWidget* p_Parent = nullptr)
            : QOpenGLWidget(p_Parent)
        {
            QSurfaceFormat newFormat = format();
            newFormat.setSwapInterval(0);
            setFormat(newFormat);
        }
    
    protected:
        virtual void initializeGL() override
        {
            initializeOpenGLFunctions();
        }
    
        virtual void paintGL() override
        {
            glClearColor(0, 0, 0, 1);
            glClear(GL_COLOR_BUFFER_BIT);
    
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glColor4f(1, 0, 0, 0.5);
            glRectf(-0.5, -1, 0.5, 1);
        }
    };
    
    class MyDialog : public QDialog
    {
    public:
        MyDialog(QWidget* p_Parent = nullptr)
            : QDialog(p_Parent, Qt::FramelessWindowHint | Qt::Dialog)
        {
            MyOpenGLWidget* myOpenGLWidget = new MyOpenGLWidget(this);
    
            setAttribute(Qt::WA_TranslucentBackground, false);
            QHBoxLayout* layout = new QHBoxLayout;
            layout->addWidget(myOpenGLWidget);
            setLayout(layout);
    
            setMinimumSize(400, 400);
        }
    
        virtual ~MyDialog()
        {
        }
    };
    
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        QPushButton* btn = new QPushButton("Press Me!");
    
        QHBoxLayout* layout = new QHBoxLayout;
        layout->addWidget(btn);
        setLayout(layout);
    
        setMinimumSize(500, 500);
    
        connect(btn, &QPushButton::clicked, [this]
        {
            MyDialog dialog;
            dialog.exec();
        });
    }
    
    Widget::~Widget()
    {
    }
    

    It seems to me that the usage of QSurfaceFormat::setSwapInterval(0) together with Qt::FramelessWindowHint is causing this issue. Changing either one of them eliminate the problem. Could anyone please advice on what I am doing wrong here? Thanks in advance.

    I am using Qt-5.15.2 and on Mac OS 10.15.7

    Compressed.png



  • For now, I manage to fix this by changing

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    

    to

    glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
    glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE);
    

    However, I would still like to understand the root cause or better alternative to fix this issue.


Log in to reply