Important: Please read the Qt Code of Conduct -

use of colors/palettes/stylesheets

  • I've been asked to add some color to my app, and am trying to understand how to do this.

    • Do I understand correctly that I should be using the palette property of the QWidget object, rather than trying to control colors on individual items?
    • as an example, how do I set the color of my main widget (ie, the background)?
    • our existing UI shows buttons. The color of the buttons changes when hovered over, and changes again when pressed. Is this also handled through the palette? I can't find anything on this other than manually using signals to change the color, which seems a little extreme.


  • Qt Champions 2017

    What OS is this? Because some systems doesn't use the palette for some of the painting and it's kind of messy. Alternatively, you could try to set a stylesheet to your app.

  • @kshegunov I'm developing on Windows 10, but any solution needs to work across platforms. Does this mean I need to use stylesheets? Somehow, I had assumed that Qt would give me a little more control over this.

  • @mzimmers
    If you want just colors - palette will work most of the time , at least for desktop systems,
    but you will have to test application on the system you want to support anyway.

    Stylesheets give additional controls (shapes, positioning, metc ) , but not all widgets and aspects can be customized.
    And this likely will not be ever fixed - due QWidget development status.
    In this case you will need customize QWidgets painting for the affected widgets.

    Warning! Palettes and stylesheet do not play together well, choose one or go QML and define everything yourself.

  • @alex_malyu thanks for the input. I'll look through the examples to see if I can find anything similar to what I want to do, and probably come back with more questions.

    Is there an unofficial guideline for when to use CSS vs. palettes?

  • Qt Champions 2017

    @mzimmers said in use of colors/palettes:

    Does this mean I need to use stylesheets?

    Most probably, but someone might prove me wrong.

    Is there an unofficial guideline for when to use CSS vs. palettes?

    Not that I'm aware of. Perhaps you could try the wiki?

  • Hi

    i would prefer you to use stylesheet for this as it provides you all the properties like button hover etc.
    for example
    "border: 1px solid #333333;"
    "color: rgb(212, 212, 212);"
    "border-radius: 2px;"
    "background-color: #116a06;"
    "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 0.67, "
    "stop: 0 #444444, stop: 1 #222222);"
    "QPushButton:pressed {"
    "border: 1px solid #333333;"
    "color: rgb(212, 212, 212);"
    "background-color: #222222;"
    "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 0.67, "
    "stop: 0 #444444, stop: 1 #222222);"

  • Hi @mzimmers

    if you got solution, please mark it as solved.

  • @Venkatesh-V I still need to do some more looking at palettes before I close this topic. Thanks for your contribution, though. It was very helpful.

  • Hi @mzimmers

    For me ,i highly recommend you using stylesheet instead of palette ,

    Stylesheet is really simple to use , and is more flexible if you want to other features , to the style of your UI.

    You can also see what the Qt Doc says about that :

    Style sheets let you perform all kinds of customizations that are difficult or impossible to perform using QPalette alone. If you want yellow backgrounds for mandatory fields, red text for potentially destructive push buttons, or fancy check boxes, style sheets are the answer.

  • @mostefa I think I'm going to do just that. I've been playing with stylesheets, and so far they meet my (modest) needs. Thanks.

  • I’ll add my two pence (or cents) and say that I’ve extensively used style sheets throughout my Qt Widgets application and the results have been excellent.

    I’ve been able to successfully apply styles to 70% of the standard widgets with the other 30% needing just a little tweaking.

Log in to reply