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. Making a translucent window, opaque contents
Forum Updated to NodeBB v4.3 + New Features

Making a translucent window, opaque contents

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 2.6k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    nicky j
    wrote on last edited by
    #1

    Hello,

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

    Thanks!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nicky j
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

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

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nicky j
            wrote on last edited by
            #5

            hmm that doesn't seem to do anything

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              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);@
              

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nicky j
                wrote on last edited by
                #7

                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?

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  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.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    nicky j
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0

                    • Login

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