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

I have a window with two buttons.

I would like to decorate each with a different stylesheet. Of course, they have different object names, but it seems that only a common selector works QPushButton.

I tried:

QPushButton#myBtnObjectName1 {

/* style definitions */

}
QPushButton#myBtnObjectName2 {

/* style definitions */

}

Tried the same with replacing #with .or only with #myBtnObjetNameX. Nothing works. Simply:

QPushButton {
/* style definitions */
}

Am I using the wrong syntax? Or is it simply impossible without getting the code QPushButtonin the code and using a separate class name for each?

+3
source share
3 answers

And yes, you must also specify "AccessibleName" in Qt Designer, not just "ObjectName"

+3
source

" " Qt Designer. qss:

:

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

:

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

To match instances with objectName, you can also use a selector ^=. According to standard :

[att ^ = val] Represents an element with the attribute att, whose value begins with the prefix "val".

Example in Qt:

QPushButton[objectName^="push"] { background-color: red; }

A QPushButton, called pushButton, will match, but not an object with a name pbt.

0
source

Source: https://habr.com/ru/post/1790525/


All Articles