Important: Please read the Qt Code of Conduct -

Problem of style sheet when a part is in a QSS file and another part set by setStyleSheet

  • Hi,
    In a QSS file, used at application level, I'm setting rules to display QToolButton. On of these rules is:

        border: 2px solid #867692;

    Now, I have one QToolButton that should use this rule. But it must also be displayed with a specific background color. This color is determined programatically. So, I tried to set it by using


    Whatever the string I'm using in setStyleSheet, the rule in the QSS file is no more applied. I tried "background-color: mycolor;", "QToolButton {bacground-color: myColor;}", ...

    Any idea on how to solve this problem?


  • Lifetime Qt Champion


    Please show the stylesheet you are using for your button.

  • Hi @SGaist,
    It is what I have written in my post. To be more precise all QToolButton have a border of a given color when they are checked (if they are checkable obviously), and no border if they are not checked. Border, and border color must be set by the QSS file to be able to be changed easily. And, usually they are also in a constant background color.

    But, this specific QToolButton must have a specific background color. And this color depends of an internal configuration of the application. This is why I'm trying to set it by setStyleSheet. Without the setStyleSheet, this button follows the rules defined in the QSS. But, with the setStyleSheet it is not the case (I always have a button with the right background but without the border when checked).

  • Ok,
    I found the bullshit. My first try was setStyleSheet("background-color: myColor;"). As it did not work (no border when checked), I tried after setStyleSheet("QToolButton {background-color: myColor;}"). As it was still the same shit, and as the documentation recommends to set the border, I have moved to setStyleSheet("QToolButton {background-color: myColor; border:none;}") (as in the QSS file). And all the following tries were with "border:none" on the rule without the :checked modifier.

    Suprisingly, without the definition of the border in the setStyleSheet everything works well. So I have the border defined in the QSS file (as from the beginning). And in the program I have:

    currentBtn  ->setStyleSheet(QString("QToolButton#BooleanObject {background-color: %1;} QToolButton#BooleanObject:checked {background-color:%1;}").arg(;

    This is a really strange behavior. But, long time ago, I have understood that I will never understand the "logic" of the QT style sheet.