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. Transparent QOpenGLWidget in Dialog
QtWS25 Last Chance

Transparent QOpenGLWidget in Dialog

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

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sivan
      wrote on last edited by
      #2

      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.

      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