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

泪湿孤枕 提交于 2020-07-08 14:00:27

问题


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?


回答1:


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;
}



回答2:


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

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

Example in Qt:

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

A QPushButton called pushButton would be matched, but not an object called pbt.




回答3:


Ah yes, the "AccessibleName" in Qt Designer needs to be set too, not just "ObjectName"



来源:https://stackoverflow.com/questions/4925184/qt-stylesheet-syntax-targeting-a-specific-button-not-all-buttons

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!