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. Changing the background color of the QMainWindow at runtime.
Forum Updated to NodeBB v4.3 + New Features

Changing the background color of the QMainWindow at runtime.

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 28.5k 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
    nitks.abhinav
    wrote on last edited by A Former User
    #1

    Hi,

    I have a class derived from QMainWindow and other widgets derived from QWidget.
    I kept the background color of main window to be gray within ctor as below:

     setStyleSheet("background-color:gray;");
     setAutoFillBackground( true );
    

    All the widgets take the background color from main window. Is is possible to change the background color at run time, i.e. while switching between widgets?

    I tried the same above commands , but It show the one set in ctor.

    Thank you,

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      When you say
      background-color:gray;

      it means for all widget and its children (and so on) since you are not using a specifier
      like name or type. Since most are children of the main window, they all get the color.

      What you can do is use names.
      If you name your widget windows ( using setObjectName() )
      you can give each the one you want.

      http://doc.qt.io/qt-5/stylesheet-examples.html

      QWidget#NAME {
          background-color: red;
      }
      
      QWidget#OHERNAME {
          background-color: blue;
      }
      
      

      Im not sure what you mean by switching widgets if its not as windows?

      Can you explain the effect you are after?

      N 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        When you say
        background-color:gray;

        it means for all widget and its children (and so on) since you are not using a specifier
        like name or type. Since most are children of the main window, they all get the color.

        What you can do is use names.
        If you name your widget windows ( using setObjectName() )
        you can give each the one you want.

        http://doc.qt.io/qt-5/stylesheet-examples.html

        QWidget#NAME {
            background-color: red;
        }
        
        QWidget#OHERNAME {
            background-color: blue;
        }
        
        

        Im not sure what you mean by switching widgets if its not as windows?

        Can you explain the effect you are after?

        N Offline
        N Offline
        nitks.abhinav
        wrote on last edited by
        #3

        @mrjj

        Thanks for the suggestion, will try to use names.

        mrjjM 1 Reply Last reply
        1
        • N nitks.abhinav

          @mrjj

          Thanks for the suggestion, will try to use names.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @nitks.abhinav

          Did you mean widgets windows or just widgets within mainwindow?

          N 1 Reply Last reply
          0
          • mrjjM mrjj

            @nitks.abhinav

            Did you mean widgets windows or just widgets within mainwindow?

            N Offline
            N Offline
            nitks.abhinav
            wrote on last edited by
            #5

            @mrjj

            Its widgets within window.

            mrjjM 1 Reply Last reply
            0
            • N nitks.abhinav

              @mrjj

              Its widgets within window.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @nitks.abhinav

              Ok. but when you say dynamic, do you mean to change the background-color when you click on
              a widget or did you just mean different colors from start
              and not changing when interacting with the widgets?

              N 1 Reply Last reply
              0
              • mrjjM mrjj

                @nitks.abhinav

                Ok. but when you say dynamic, do you mean to change the background-color when you click on
                a widget or did you just mean different colors from start
                and not changing when interacting with the widgets?

                N Offline
                N Offline
                nitks.abhinav
                wrote on last edited by
                #7

                @mrjj

                Its like initially will show some gif animation as part of the mainwindow, when its over will show a widget. This widget after animation gets color from parent as you told, but I need to change the color.

                mrjjM 1 Reply Last reply
                0
                • N nitks.abhinav

                  @mrjj

                  Its like initially will show some gif animation as part of the mainwindow, when its over will show a widget. This widget after animation gets color from parent as you told, but I need to change the color.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @nitks.abhinav
                  Ok. Names can do for you.
                  However, depending what the widgets are, you might discover that they change looks.
                  (like QPushButton) when you apply styles to them.
                  If just a plain widget, it might be ok.
                  If its issue, you should look into using palette to just change bg color.
                  (in widget ctor)
                  QPalette pal = this->palette();
                  pal.setColor(QPalette::Window ( might use other role) , Qt::white);
                  this->setPalette(pal);

                  N 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @nitks.abhinav
                    Ok. Names can do for you.
                    However, depending what the widgets are, you might discover that they change looks.
                    (like QPushButton) when you apply styles to them.
                    If just a plain widget, it might be ok.
                    If its issue, you should look into using palette to just change bg color.
                    (in widget ctor)
                    QPalette pal = this->palette();
                    pal.setColor(QPalette::Window ( might use other role) , Qt::white);
                    this->setPalette(pal);

                    N Offline
                    N Offline
                    nitks.abhinav
                    wrote on last edited by
                    #9

                    I tried both the changes, none worked.:

                    SysInfoGUI::SysInfoGUI(QWidget *parent) :
                        QWidget(parent),
                        ui(new Ui::SysInfoGUI)
                    {
                        ui->setupUi(this);
                        QPalette pal = palette();
                        pal.setColor(QPalette::Window, Qt::black);
                        this->setPalette(pal);
                        this->setObjectName("sysinfogui");
                    }
                    

                    as well tried using name within main window function, name "sysinfogui" defined in ctor as above:

                    setStyleSheet("QWidget#sysinfogui {background-color:black}");
                    setAutoFillBackground( true );
                    stackedWidget->setCurrentIndex(3);
                    
                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      Hi
                      Stylesheets are cascading so its best to set on mainwindow only and control it from one point.
                      Also if you still have no name to target mainwindow, it still affects all.
                      (to apply in Designer, right click mainwindow (in list of names) and select Edit Style Sheet)

                      alt text

                      Best way is studying the docs as stylesheets are not trivial and
                      it takes some practice due to the cascading effect.

                      Note that mainwindow is a bit special as its actually a widget called centralWidget
                      you see in center. (see in example)

                      N 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        Hi
                        Stylesheets are cascading so its best to set on mainwindow only and control it from one point.
                        Also if you still have no name to target mainwindow, it still affects all.
                        (to apply in Designer, right click mainwindow (in list of names) and select Edit Style Sheet)

                        alt text

                        Best way is studying the docs as stylesheets are not trivial and
                        it takes some practice due to the cascading effect.

                        Note that mainwindow is a bit special as its actually a widget called centralWidget
                        you see in center. (see in example)

                        N Offline
                        N Offline
                        nitks.abhinav
                        wrote on last edited by
                        #11

                        @mrjj

                        There was some issue, now setStyleSheet is working. Thanks.

                        setStyleSheet("background-color:blck");
                        
                        1 Reply Last reply
                        1

                        • Login

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