Important: Please read the Qt Code of Conduct -

Making a translucent window, opaque contents

  • Hello,

    How could I make my QMainWindow translucent but keep all the contents of it at their normal opacity?


  • Qt Champions 2017

    In summary use the stylesheet way of allowing the properties to child or not. One way of doing is that
    Use the setObjectName("myObjectName") then use object name selector # in the css.

  • ok so this is what I have:

    @mainView::mainView(QWidget *parent)
    : QMainWindow(parent)

    It sets the mainView and everything within it to 0.8 opacity. How can I set it to that opacity, but keep all the contents at 1.0 opacity?

  • Qt Champions 2017

    Try this.
    @ this->setObjectName("myname");
    QString style = "QMainWindow#myname { opacity:0.8 }";

  • hmm that doesn't seem to do anything

  • Qt Champions 2017

    I tried this and it works. It may be issue with your Window Size. Just try with following code.

    QString style = "QMainWindow#myname { background-color: yellow }";

     QPushButton *button = new QPushButton("Hello");
     QString style1 = "QPushButton#mybutton { color: red }";

  • Ok so your second example works, but I still can't change the opacity. I tried "background: transparent" but it made the background black. What else can I try?

  • Moderators

    i don't know what you want exactly, but making a window background transparent is only possible when you do the following:
    QVBoxLayout* layout = new QVBoxLayout;
    layout->addWidget(new QComboBox);
    layout->addWidget( new QSlider );
    QWidget* mainWidget = new QWidget;

    QMainWindow mainWin;
        mainWin.setCentralWidget( mainWidget );
        mainWin.setWindowFlags(Qt::FramelessWindowHint);     //<<<<<<<<<
        mainWin.setAttribute(Qt::WA_TranslucentBackground);      //<<<<<<<<<
                    "QMainWindow#main {"
                    "background: transparent;"


    Note the setting of the window flags and widget attribute.

  • Yeah I've used that type of code before, and it just makes what should be transparent black. Is it possible that this is a limitation of OS X?

Log in to reply