Qt Stylesheet syntax: targeting a specific button, not ALL buttons

JasonGenX picture JasonGenX · Feb 7, 2011 · Viewed 13.2k times · Source

I have a window with two buttons.

I'd like to decorate each one with a different stylesheet. They both have different object names, of course, but it seems that only the generic QPushButton stylesheet selector works.

I tried:

QPushButton#myBtnObjectName1 {

/* style definitions */

}
QPushButton#myBtnObjectName2 {

/* style definitions */

}

Tried the same with replacing the # with a ., or having the #myBtnObjetNameX only. Nothing works. Just:

QPushButton {
/* style definitions */
}

Am I using a wrong syntax? Or is this simply impossible without deriving from QPushButton in code and using a separate class name for each?

Answer

Lukas picture Lukas · Sep 14, 2015

You can use "accessibleName" in Qt Designer for this. And in qss stylesheet:

more universal:

[accessibleName="alert-error"] {
    color: red;
}

or be more specific:

QPushButton[accessibleName="bigred"] {
   background-color: red;
}