3

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:

enter image description 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?

  • Please provide a simple minimal reproducible example that can be used to reproduce the problem. Keep in mind that the issue may be platform dependent so any information re. OS, Qt/compiler version etc. should also be provided. – G.M. Jun 8 at 18:19
  • @eyllanesc I'll look to post a minimal example. Otherwise, the platform is Windows 7 64-bit, Qt 5.5.1 MSCV2010, Compiler: MS Windows SDK for Windows 7 (7.1.7600.0.30514)(x86). Application compiled as 32-bit. – TheShiftingInt Jun 11 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.