How to make WA_TranslucentBackground work on Windows without using FramelessWindowHint?
-
wrote on 13 Feb 2023, 00:16 last edited by Roberrt
Im using
setAttribute(Qt::WA_TranslucentBackground);
to make my MainWindow background transparent, however, this attribute only work if:I also set:
setWindowFlag(Qt::FramelessWindowHint);
or
Have at least onceQOpenGLWidget
in my GUI.I tried asking ChatGPT what happens when i add
QOpenGLWidget
to my window which makes the attribute to work:Qt::WA_TranslucentBackground is an attribute that sets the background of a widget to be translucent, meaning that any widgets or windows underneath it will be visible. The attribute needs to be set on the top-level window for it to work properly. However, for a translucent background to be properly rendered, the window needs to have an alpha channel, which is a channel in the color format that stores the transparency information for each pixel. In the case of a QMainWindow, the default color format doesn't include an alpha channel. When you add a QOpenGLWidget to your QMainWindow, it sets the required format and creates a context for OpenGL rendering. This provides an alpha channel for the window, allowing the translucent background to be rendered correctly. So, the reason Qt::WA_TranslucentBackground only works when you add a QOpenGLWidget to your QMainWindow is that the QOpenGLWidget is providing the required alpha channel for the translucent background to be rendered correctly. ------------------------------------------------------------------------------------------------ Q) Is possible to provide this required alpha channel without including a QOpenGLwidget to it? ------------------------------------------------------------------------------------------------ Yes, it is possible to provide the required alpha channel for a translucent background without including a QOpenGLWidget. One way to do this is by setting the window surface format for your QMainWindow explicitly. You can do this by creating a QSurfaceFormat object, setting the required format options (e.g. an alpha channel), and then setting this format as the window surface format for your QMainWindow using the QSurfaceFormat::setDefaultFormat() static function.
I exhaustedly tried all answers it gave, specially using
QSurfaceFormat
, but the background remains black.I have no experience with
QOpenGlWidget
, i would like to ask if is it really possible to get this attribute working usingQSurfaceFormat
or something like;Or if someone knows any other 'option' than setting the
FramelessWindowHint
flag or creating an QOpenGL widget.Example code:
int main(int argc, char *argv[]) { QApplication a(argc, argv); /* QSurfaceFormat format; format.setAlphaBufferSize(8); QSurfaceFormat::setDefaultFormat(format); QSurfaceFormat format; format.setAlphaBufferSize(8); format.setSamples(16); format.setSwapBehavior(QSurfaceFormat::DoubleBuffer); format.setRenderableType(QSurfaceFormat::OpenGL); format.setProfile(QSurfaceFormat::CoreProfile); QSurfaceFormat::setDefaultFormat(format); */ MainWindow w; w.show(); return a.exec(); } //mainwindow.h #include <QtWidgets/QMainWindow> #include "ui_MainWindow.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindowClass; }; QT_END_NAMESPACE class MainWindow : public QMainWindow{ Q_OBJECT public: MainWindow(QWidget* parent = nullptr); } //mainwindow.cpp MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowClass()) { ui->setupUi(this); setAttribute(Qt::WA_TranslucentBackground); QGridLayout* layout = new QGridLayout(); ui->centralWidget->setStyleSheet("background-color: transparent;"); ui->centralWidget->setLayout(layout); QWidget* widget = new QWidget(this); widget->setStyleSheet("background-color: green; border-radius: 32px;"); layout->addWidget(widget); //QOpenGLWidget* opengl = new QOpenGLWidget(this); //opengl->deleteLater(); }
Creating the
QOpenGlWidget
and callingopengl->deleteLater()
theWA_TranslucentBackground
works, however, it make the application use 50mb more of ram compared to not creating theQOpenGLWidget
.This is not much if i were creating just one GUI, but I'm creating multiples.
1/1