How to draw opaque background with WA_TranslucentBackground active
Edit: forgot to add - I am using Qt 5.15 under Windows 10 MSYS2 MinGW
I have a QMainWindow subclass with
Code:
setAttribute(Qt::WA_TranslucentBackground);
in its constructor. I want the widgets to be transparent by default, which works. But now I want to add a special widget, say the member "QWidget* m_opaque;", with a fixed non-transparent background color. However even with
- m_opaque->setAutofillBackground(true);
- m_opaque->setAttribute(Qt::WA_OpaquePaintEvent);
- m_opaque->setWindowOpacity(1.0);
- m_opaque->setStyleSheet("{ background-color: grey; }");
the widget still has transparent background .. how do I make it opaque?
Re: How to draw opaque background with WA_TranslucentBackground active
Have you tried this?
Code:
m_opaque->setAttribute( Qt::WA_TranslucentBackground, false );
Re: How to draw opaque background with WA_TranslucentBackground active
After trying to fix my other issue, I finally tried your answer
Code:
m_opaque->setAttribute( Qt::WA_TranslucentBackground, false );
but this does not work either. To be specific, I am trying to show the status bar from my other question with grey brackground. Currently, if I use
Code:
setAttribute(Qt::WA_TranslucentBackground);
in my main window constructor, nothing I do seems to be able to get rid of the transparency.
Furthermore, I remembered that already noticed before that after show(), nothing I tried was able to change this attribute / transparency setting (and vice-versa: after show(), activating WA_TranslucentBackground does nothing). My only way out to go from transparent to non-transparent widgets in my window was to destroy the whole window and create a new one. However now I need a QLabel with transparency, and a QStatusBar without transparency in the same window ...
I also found some bug reports about seemingly similar issues, but nothing that looked exactly the same as mine, so not sure what the status on this is.
Edit: turns out my style sheet syntax was wrong .. the following
Code:
m_opaque->->setStyleSheet("background-color: grey");
works. Still strange none of the other options show any affect, but at least I can use this.
Re: How to draw opaque background with WA_TranslucentBackground active
Quote:
nothing I tried was able to change this attribute / transparency setting
Yes, I think that is correct - the attribute can only be changed when the widget has been constructed but before it is shown. Probably has something to do with the underlying window system and the way it creates the native windows wrapped by QWidget / QWindow. It is during the transition from constructing the QWidget container to showing it on screen that the underlying native window gets instantiated and configured and there are properties of the native window that can't be changed once that happens.