Important: Please read the Qt Code of Conduct -

[SOLVED] setAttribute and setBackgroundRole. Trying to set a dark background

  • Hello!

    I've tried to set the background colour with


    But didn't work. Then I tried with


    And worked (at least it showed a dark screen...). I've read "this":, and the "docs":, but I don't know why the first approach doesn't work. By the way, the doc say:

    bq. You can modify the palette or set a style sheet if you don't achieve the result you want with setBackgroundRole()

    If it wouldn't work, why? Somebody knows? Is there any difference between showing no background and setting a dark one?


  • Hi QMartin,

    The setBackgroundRole method let you use a color role for the background, which means one of the predefined color of the style applied to the widget. So your are basically limited to the style and its colors.

    So if you don't achieve the result you want (i.e. if the style applied don't have the color/brush you want), use palette or style sheets.

    Here is the style sheet way
    myWidget->setStyleSheet("* { background-color: rgb(50, 50, 50); }");

    This will change your widget background color to a dark gray.

  • Wow, I think this was the fastest solved thread :D

    Thanks a lot! Best regards!

  • You're welcome ! :)

Log in to reply