Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make WA_TranslucentBackground work on Windows without using FramelessWindowHint?
Forum Updated to NodeBB v4.3 + New Features

How to make WA_TranslucentBackground work on Windows without using FramelessWindowHint?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 651 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Roberrt
    wrote on 13 Feb 2023, 00:16 last edited by Roberrt
    #1

    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 once QOpenGLWidget 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 using QSurfaceFormat 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 calling opengl->deleteLater() the WA_TranslucentBackground works, however, it make the application use 50mb more of ram compared to not creating the QOpenGLWidget.

    This is not much if i were creating just one GUI, but I'm creating multiples.

    1 Reply Last reply
    0

    1/1

    13 Feb 2023, 00:16

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved