4

I would like to set my QMainWindow class semi-transparent background using QMainWindow::setStyleSheet method. I do something like:

QMainWindow window;
window.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
window.setStyleSheet("background-color: rgba(255, 0, 0, 128)");
window.setAttribute(Qt::WA_TranslucentBackground, true);
window.setFixedSize(800, 600);
window.show();

and I got fully transparent Window, which is pretty much nothing I can see. and if I do it without

window.setAttribute(Qt::WA_TranslucentBackground, true);

I got fully red Window.

I found out, inheriting 'QMainWindow', overloading 'paintEvent()' and use 'QPainter->fillRect()' with QColor with alpha do what I want, but it's not using stylesheet.

Anyone can help doing this using 'setStyleSheet()' method? I've already found lots of posts and answers, but nothing helped me.

Best regards.

Fryderyk
  • 61
  • 1
  • 7

1 Answers1

2

It looks like i found the solution by accident. Have to create QWidget, set is as central widget on QMainWindow and set stylesheet on the widget not main window. Works just fine.

QMainWindow window;
window.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
window.setAttribute(Qt::WA_TranslucentBackground, true);
window.setFixedSize(800, 600);

QWidget widget(&window);
widget.setStyleSheet("background-color: rgba(255, 0, 0, 128)");

window.setCentralWidget(&widget);
window.show();
Fryderyk
  • 61
  • 1
  • 7