[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"));
    else
        p.setColor(QPalette::Window, QColor("green"));
    
    childLcd->setPalette(p);
    childLcd->display(value);
    

    }
    @

    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
    @
    childLcd->setAutoFillBackground(true)
    @

    Works now!


Log in to reply
 

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