Changing the background color of the QMainWindow at runtime.



  • 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,


  • Qt Champions 2016

    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?



  • @mrjj

    Thanks for the suggestion, will try to use names.


  • Qt Champions 2016

    @nitks.abhinav

    Did you mean widgets windows or just widgets within mainwindow?



  • @mrjj

    Its widgets within window.


  • Qt Champions 2016

    @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?



  • @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.


  • Qt Champions 2016

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



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

  • Qt Champions 2016

    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)



  • @mrjj

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

    setStyleSheet("background-color:blck");
    

Log in to reply
 

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