How to change the opacity of Qt MainWindow?

3.2k views Asked by At

How to change the opacity of the Qt MainWindow by some values?

My main window contains nothing but just somehow I need to change the opacity using keystrokes.

It looks like this now.

enter image description here

I tried to set using this->setWindowOpacity(0.5);

didn't get anything opacity effect.

enter image description here

2

There are 2 answers

0
Alexander V On BEST ANSWER

The below works for me most of times (as long as we can run in stylesheet override problem with other ways). Consider change the last component of rgba to less than 255 for making it semi-transparent.

widget->setStyleSheet("background-color: rgba(255, 255, 255, 255);");

Mind that child widgets may inherit the transparent background of parent widget by default unless you specify "background-color: rgba(255, 255, 255, 0);" for them or limit the visibility of upper stylesheet somehow e.g. "QMainWindow(background-color: rgba(255, 255, 255, 255);}".

0
bmeric On

You can change the windowOpacity property using setProperty.

this->setProperty("windowOpacity", 0.5);