정리

Style Sheet Syntax : Selector

저장소/Qt


Qt Style Sheet로 이것저것 해보는 중에 차이가 잘 이해되지 않는 것

이것과
".QWidget {"
"background-color: rgba(0, 0, 0, 100%); }"
이것의
"QWidget {"
"background-color: rgba(0, 0, 0, 100%); }"
차이점은?

결론은 나만 적용할 것인가, 하위 위젯에도 동일하게 적용할 것인가의 차이.



Qt Centre에 아래 질문 올라옴.
http://www.qtcentre.org/threads/30221-Style-Sheet-Syntax-background
    Q: Style Sheet Syntax background


I want to have the background only from a QWidget black. The background from the QPushbuttons, QSliders etc. shoud be normal.

I use the Style-Syntax:


QWidget {

background-color:rgb(20, 20, 20);

}


I read in the Qt Assistant (Stylesheet-Syntax) that i must make bevore the QWidget a point:


.QWidget {

background-color:rgb(20, 20, 20);

}


I have now the problam that this didn't work. 


Thanks for help.



A:

well it works, but only for QWidgets! No subclass, even not your own if they only inherit QWidget.


maybe you provide a small executable example showing your problem.





The Style Sheet Syntax

http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html

Selector Types

All the examples so far used the simplest type of selector, the Type Selector. Qt Style Sheets support all the selectors defined in CSS2. The table below summarizes the most useful types of selectors.

SelectorExampleExplanation
Universal Selector*Matches all widgets.
Type SelectorQPushButtonMatches instances of QPushButton and of its subclasses.
Property SelectorQPushButton[flat="false"]Matches instances of QPushButton that are not flat. You may use this selector to test for any Qt property that supports QVariant::toString() (see the toString()function documentation for details). In addition, the special class property is supported, for the name of the class.

This selector may also be used to test dynamic properties. For more information on customization using dynamic properties, refer toCustomizing Using Dynamic Properties.

Instead of =, you can also use ~= to test whether a Qt property of typeQStringList contains a given QString.

Warning: If the value of the Qt property changes after the style sheet has been set, it might be necessary to force a style sheet recomputation. One way to achieve this is to unset the style sheet and set it again.

Class Selector

.QPushButton

Matches instances of QPushButton, but not of its subclasses.

This is equivalent to *[class~="QPushButton"].

ID SelectorQPushButton#okButtonMatches all QPushButton instances whose object name is okButton.
Descendant SelectorQDialog QPushButtonMatches all instances of QPushButton that are descendants (children, grandchildren, etc.) of a QDialog.
Child SelectorQDialog > QPushButtonMatches all instances of QPushButton that are direct children of a QDialog.