Important: Please read the Qt Code of Conduct -

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:

     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,

  • Lifetime Qt Champion

    When you say

    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.

    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.

  • Lifetime Qt Champion


    Did you mean widgets windows or just widgets within mainwindow?

  • @mrjj

    Its widgets within window.

  • Lifetime Qt Champion


    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.

  • Lifetime Qt Champion

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

  • I tried both the changes, none worked.:

    SysInfoGUI::SysInfoGUI(QWidget *parent) :
        ui(new Ui::SysInfoGUI)
        QPalette pal = palette();
        pal.setColor(QPalette::Window, Qt::black);

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

    setStyleSheet("QWidget#sysinfogui {background-color:black}");
    setAutoFillBackground( true );

  • Lifetime Qt Champion

    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.


Log in to reply