I would like to have an opaque QWidget in a transparent QMainWindow. The QMainWindow's centralWidget contains a QWidget and a QOpenGLWidget in a vertical layout. The QOpenGLWidget is drawing correctly with rendered objects being opaque and the background as transparent. However, the QWidget is drawn with partial transparency which is not what I would like.
An example of the application over a black and red background is shown here:
The problem is the top QWidget (toolbar with buttons). This should not show the red or black through it but instead show a solid opaque background colour. The QOpenGLWidget should show the red/black through as it currently is.
The screenshot was taken with the following code in the MainWindow:
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_NoSystemBackground, false);
The QOpenGLWidget::paintGL() contains:
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
I have tried various calls on different widgets without success, including:
setStyleSheet(...)
setAutoFillBackground(...)
setAttribute(Qt::WA_AlwaysStackOnTop)
setAttribute(Qt::WA_TranslucentBackground)
setAttribute(Qt::WA_NoSystemBackground, false)
setWindowOpacity(1.0)
I have also somewhat achieved the desired effect by setting the QWidget to have a null parent, setting it as frameless, and positioning it manually over the MainWindow. But this is messy code wise and results in two application icons being shown in the Windows taskbar. I would not like to go this hackish route.
Is there a way to achieve this by setting the correct properties of the QWidget toolbar?
Update: A minimal code example project can be found here: https://github.com/peguse/TransparentQOpenGLWidgetApp