Nominate our 2022 Qt Champions!

[Solved] How to override a child QWidget's background colour?

  • Moderators

    Hi all,

    I'm trying to make my QLCDNumber change its background colour according to its value.
    void ParentWidget::updateLcd(int value)
    QPalette p;

    if (value > 10)
        p.setColor(QPalette::Window, QColor("red"));
        p.setColor(QPalette::Window, QColor("green"));


    This works if the QLCDNumber is created as a top-level widget, but not if it's made a child of ParentWidget -- it seems to be forced to use its parent's palette.

    What options do I have?

    I've also tried childLcd->setPalette(Qt::red) -- that does work, but it only colours the borders, not the whole background.

  • Moderators

    Ah, apparently I need to call

    Works now!

Log in to reply