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?

    Thanks!



  • 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)
    {
    this->setWindowOpacity(0.8);@

    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?



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



  • hmm that doesn't seem to do anything



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

    @
    this->setObjectName("myname");
    QString style = "QMainWindow#myname { background-color: yellow }";
    this->setStyleSheet(style);

     QPushButton *button = new QPushButton("Hello");
     button->setObjectName("mybutton");
     QString style1 = "QPushButton#mybutton { color: red }";
     button->setStyleSheet(style1);@


  • 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;
    mainWidget->setLayout(layout);

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

    @

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.