Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Set color of QWidget



  • Hellow, i have one problem:
    I need to set color of QWidget, how can i do it without QSS
    I try
    @ QPalette * p = new QPalette;
    p->setColor(QPalette::Window, Qt::red);
    w->setPalette(*p);@
    @ QPalette * p = new QPalette;
    p->setColor(QPalette::Background, Qt::red);
    w->setPalette(*p);@
    @ QPalette * p = new QPalette;
    p->setColor(w->foregroundRole() , Qt::red);
    w->setPalette(*p);@
    @ QPalette * p = new QPalette;
    p->setColor(w->backgroundRole() , Qt::red);
    w->setPalette(*p);@
    w is a object of QWidget
    But it is all doesnt work



  • Instead you can try to do it using stylesheets. It should be something like this:
    @
    w->setStyleSheet("background-color:red;");
    @

    Edit: Sorry I saw that you do not want to use QSS too late.

    @
    QPalette p(palette());
    p.setColor(QPalette::Background, Qt::red);
    w->setAutoFillBackground(true);
    w->setPalette(p);
    @

    For more details please check "this article":http://developer.qt.nokia.com/wiki/How_to_Change_the_Background_Color_of_QWidget



  • Many thanks for your help



  • You are welcome! Sorry again for my first proposition - I rushed to answer without having read the question carefully :)


Log in to reply