Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
stylesheet-syntax.qdoc
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page stylesheet-syntax.html
6 \previouspage Qt Style Sheets
7 \nextpage Qt Widgets Designer Integration
8 \title The Style Sheet Syntax
9
10 Qt Style Sheet terminology and syntactic rules are almost
11 identical to those of HTML CSS. If you already know CSS, you can
12 probably skim quickly through this section.
13
14 \section1 Style Rules
15
16 Style sheets consist of a sequence of style rules. A \e{style
17 rule} is made up of a selector and a declaration. The
18 \e{selector} specifies which widgets are affected by the rule;
19 the \e{declaration} specifies which properties should be set on
20 the widget. For example:
21
22 \snippet code/doc_src_stylesheet.qdoc 1
23
24 In the above style rule, \c QPushButton is the selector and \c{{
25 color: red }} is the declaration. The rule specifies that
26 QPushButton and its subclasses (e.g., \c MyPushButton) should use
27 red as their foreground color.
28
29 Qt Style Sheet is generally case insensitive (i.e., \c color,
30 \c Color, \c COLOR, and \c cOloR refer to the same property).
31 The only exceptions are class names,
32 \l{QObject::setObjectName()}{object names}, and Qt property
33 names, which are case sensitive.
34
35 Several selectors can be specified for the same declaration,
36 using commas (\c{,}) to separate the selectors. For example,
37 the rule
38
39 \snippet code/doc_src_stylesheet.qdoc 2
40
41 is equivalent to this sequence of three rules:
42
43 \snippet code/doc_src_stylesheet.qdoc 3
44
45 The declaration part of a style rule is a list of
46 \tt{\e{property}: \e{value}} pairs, enclosed in braces (\c{{}})
47 and separated with semicolons. For example:
48
49 \snippet code/doc_src_stylesheet.qdoc 4
50
51 See the \l{List of Properties} section below for the list of
52 properties provided by Qt widgets.
53
54 \section1 Selector Types
55
56 All the examples so far used the simplest type of selector, the
57 Type Selector. Qt Style Sheets support all the
58 \l{http://www.w3.org/TR/REC-CSS2/selector.html#q1}{selectors
59 defined in CSS2}. The table below summarizes the most useful
60 types of selectors.
61
62 \table 100%
63 \header
64 \li Selector
65 \li Example
66 \li Explanation
67
68 \row
69 \li Universal Selector
70 \li \c *
71 \li Matches all widgets.
72
73 \row
74 \li Type Selector
75 \li \c QPushButton
76 \li Matches instances of QPushButton and of its subclasses.
77
78 \row
79 \li Property Selector
80 \li \c{QPushButton[flat="false"]}
81 \li Matches instances of QPushButton that are not
82 \l{QPushButton::}{flat}. You may use this selector to test
83 for any Qt \l{Qt's Property System}{property} that supports
84 QVariant::toString() (see the \l{QVariant::}{toString()}
85 function documentation for details). In addition, the
86 special \c class property is supported, for the name of the
87 class.
88
89 This selector may also be used to test dynamic properties.
90 For more information on customization using dynamic properties,
91 refer to \l{Customizing Using Dynamic Properties}.
92
93 Instead of \c =, you can also use \c ~= to test whether a
94 Qt property of type QStringList contains a given QString.
95
96 \warning If the value of the Qt property changes after the
97 style sheet has been set, it might be necessary to force a
98 style sheet recomputation. One way to achieve this is to
99 unset the style sheet and set it again.
100
101 \row
102 \li Class Selector
103 \li \c .QPushButton
104 \li Matches instances of QPushButton, but not of its subclasses.
105
106 This is equivalent to \c{*[class~="QPushButton"]}.
107
108 \row
109 \li ID \target ID Selector
110 Selector
111 \li \c{QPushButton#okButton}
112 \li Matches all QPushButton instances whose
113 \l{QObject::objectName}{object name} is \c okButton.
114
115 \row
116 \li Descendant Selector
117 \li \c{QDialog QPushButton}
118 \li Matches all instances of QPushButton that are descendants
119 (children, grandchildren, etc.) of a QDialog.
120
121 \row
122 \li Child Selector
123 \li \c{QDialog > QPushButton}
124 \li Matches all instances of QPushButton that are direct
125 children of a QDialog.
126 \endtable
127
128 \section1 Sub-Controls
129
130 For styling complex widgets, it is necessary to access subcontrols of the
131 widget, such as the drop-down button of a QComboBox or the up and down
132 arrows of a QSpinBox. Selectors may contain \e{subcontrols} that make it
133 possible to restrict the application of a rule to specific widget
134 subcontrols. For example:
135
136 \snippet code/doc_src_stylesheet.qdoc 5
137
138 The above rule styles the drop-down button of all \l{QComboBox}es.
139 Although the double-colon (\c{::}) syntax is reminiscent of CSS3
140 Pseudo-Elements, Qt Sub-Controls differ conceptually from these and have
141 different cascading semantics.
142
143 Sub-controls are always positioned with respect to another element - a
144 reference element. This reference element could be the widget or another
145 Sub-control. For example, the \l{Qt Style Sheets Reference#drop-down-sub}
146 {::drop-down} of a QComboBox is placed, by default, in the top right corner
147 of the Padding rectangle of the QComboBox. The
148 \l{Qt Style Sheets Reference#drop-down-sub}{::drop-down} is placed,
149 by default, in the Center of the Contents rectangle of the
150 \l{Qt Style Sheets Reference#drop-down-sub}{::drop-down} Sub-control. See
151 the \l{List of Stylable Widgets} below for the Sub-controls to use to
152 style a widget and their default positions.
153
154 The origin rectangle to be used can be changed using the
155 \l{Qt Style Sheets Reference#subcontrol-origin-prop}{subcontrol-origin}
156 property. For example, if we want to place the drop-down in the margin
157 rectangle of the QComboBox instead of the default Padding rectangle, we
158 can specify:
159
160 \snippet code/doc_src_stylesheet.qdoc 6
161
162 The alignment of the drop-down within the Margin rectangle is changed
163 using \l{Qt Style Sheets Reference#subcontrol-position-prop}
164 {subcontrol-position} property.
165
166 The \l{Qt Style Sheets Reference#width-prop}{width} and
167 \l{Qt Style Sheets Reference#height-prop}{height} properties can be used
168 to control the size of the Sub-control. Note that setting a
169 \l{Qt Style Sheets Reference#image-prop}{image} implicitly sets the size
170 of a Sub-control.
171
172 The relative positioning scheme
173 (\l{Qt Style Sheets Reference#position-prop}{position} : relative),
174 allows the position of the Sub-Control to be offset from its initial
175 position. For example, when the QComboBox's drop-down button is
176 pressed, we might like the arrow inside to be offset to give a
177 "pressed" effect. To achieve this, we can specify:
178
179 \snippet code/doc_src_stylesheet.qdoc 7
180
181 The absolute positioning scheme
182 (\l{Qt Style Sheets Reference#position-prop}{position} : absolute),
183 allows the position and size of the Sub-control to be changed with
184 respect to the reference element.
185
186 Once positioned, they are treated the same as widgets and can be styled
187 using the \l{box model}.
188
189 See the \l{List of Sub-Controls} below for a list of supported
190 sub-controls, and \l{Customizing the QPushButton's Menu Indicator
191 Sub-Control} for a realistic example.
192
193 \note With complex widgets such as QComboBox and QScrollBar, if one
194 property or sub-control is customized, \b{all} the other properties or
195 sub-controls must be customized as well.
196
197 \section1 Pseudo-States
198
199 Selectors may contain \e{pseudo-states} that denote that restrict
200 the application of the rule based on the widget's state.
201 Pseudo-states appear at the end of the selector, with a colon
202 (\c{:}) in between. For example, the following rule applies when
203 the mouse hovers over a QPushButton:
204
205 \snippet code/doc_src_stylesheet.qdoc 8
206
207 Pseudo-states can be negated using the exclamation operator. For
208 example, the following rule applies when the mouse does not hover
209 over a QRadioButton:
210
211 \snippet code/doc_src_stylesheet.qdoc 9
212
213 Pseudo-states can be chained, in which case a logical AND is
214 implied. For example, the following rule applies to when the
215 mouse hovers over a checked QCheckBox:
216
217 \snippet code/doc_src_stylesheet.qdoc 10
218
219 Negated Pseudo-states may appear in Pseudo-state chains. For example,
220 the following rule applies when the mouse hovers over a QPushButton
221 that is not pressed:
222
223 \snippet code/doc_src_stylesheet.qdoc 11
224
225 If needed, logical OR can be expressed using the comma operator:
226
227 \snippet code/doc_src_stylesheet.qdoc 12
228
229 Pseudo-states can appear in combination with subcontrols. For
230 example:
231
232 \snippet code/doc_src_stylesheet.qdoc 13
233
234 See the \l{List of Pseudo-States} section below for the list of
235 pseudo-states provided by Qt widgets.
236
237 \section1 Conflict Resolution
238
239 Conflicts arise when several style rules specify the same
240 properties with different values. Consider the following style
241 sheet:
242
243 \snippet code/doc_src_stylesheet.qdoc 14
244
245 Both rules match QPushButton instances called \c okButton and
246 there is a conflict for the \c color property. To resolve this
247 conflict, we must take into account the \e specificity of the
248 selectors. In the above example, \c{QPushButton#okButton} is
249 considered more specific than \c QPushButton, because it
250 (usually) refers to a single object, not to all instances of a
251 class.
252
253 Similarly, selectors with pseudo-states are more specific than
254 ones that do not specify pseudo-states. Thus, the following style
255 sheet specifies that a \l{QPushButton} should have white text
256 when the mouse is hovering over it, otherwise red text:
257
258 \snippet code/doc_src_stylesheet.qdoc 15
259
260 Here's a tricky one:
261
262 \snippet code/doc_src_stylesheet.qdoc 16
263
264 Here, both selectors have the same specificity, so if the mouse
265 hovers over the button while it is enabled, the second rule takes
266 precedence. If we want the text to be white in that case, we can
267 reorder the rules like this:
268
269 \snippet code/doc_src_stylesheet.qdoc 17
270
271 Alternatively, we can make the first rule more specific:
272
273 \snippet code/doc_src_stylesheet.qdoc 18
274
275 A similar issue arises in conjunction with Type Selectors.
276 Consider the following example:
277
278 \snippet code/doc_src_stylesheet.qdoc 19
279
280 Both rules apply to QPushButton instances (since QPushButton
281 inherits QAbstractButton) and there is a conflict for the
282 \l{Qt Style Sheets Reference#color-prop}{color} property. Because QPushButton
283 inherits QAbstractButton, it might be tempting to assume that
284 \c QPushButton is more specific than \c QAbstractButton. However,
285 for style sheet computations, all Type Selectors have the same
286 specificity, and the rule that appears last takes precedence. In
287 other words, \l{Qt Style Sheets Reference#color-prop}{color} is set to \c gray
288 for all \l{QAbstractButton}s, including \l{QPushButton}s. If we really
289 want \l{QPushButton}s to have red text, we can always reorder the
290 rules.
291
292 For determining the specificity of a rule, Qt Style Sheets follow
293 the
294 \l{http://www.w3.org/TR/REC-CSS2/cascade.html#specificity}{CSS2
295 Specification}:
296
297 \quotation
298 \e{A selector's specificity is calculated as follows:}
299
300 \list
301 \li \e{count the number of ID attributes in the selector (= a)}
302 \li \e{count the number of other attributes and pseudo-classes in the selector (= b)}
303 \li \e{count the number of element names in the selector (= c)}
304 \li \e{ignore pseudo-elements [i.e., \l{subcontrols}].}
305 \endlist
306
307 \e{Concatenating the three numbers a-b-c (in a number system with a
308 large base) gives the specificity.}
309
310 \e{Some examples:}
311
312 \snippet code/doc_src_stylesheet.qdoc 20
313 \endquotation
314
315 \section1 Cascading
316
317 Style sheets can be set on the QApplication, on parent widgets,
318 and on child widgets. An arbitrary widget's effective style sheet
319 is obtained by merging the style sheets set on the widget's
320 ancestors (parent, grandparent, etc.), as well as any style sheet
321 set on the QApplication.
322
323 When conflicts arise, the widget's own style sheet is always
324 preferred to any inherited style sheet, irrespective of the
325 specificity of the conflicting rules. Likewise, the parent
326 widget's style sheet is preferred to the grandparent's, etc.
327
328 One consequence of this is that setting a style rule on a widget
329 automatically gives it precedence over other rules specified in
330 the ancestor widgets' style sheets or the QApplication style
331 sheet. Consider the following example. First, we set a style
332 sheet on the QApplication:
333
334 \snippet code/doc_src_stylesheet.cpp 21
335
336 Then we set a style sheet on a QPushButton object:
337
338 \snippet code/doc_src_stylesheet.cpp 22
339
340 The style sheet on the QPushButton forces the QPushButton (and
341 any child widget) to have blue text, in spite of the more
342 specific rule set provided by the application-wide style sheet.
343
344 The result would have been the same if we had written
345
346 \snippet code/doc_src_stylesheet.cpp 23
347
348 except that if the QPushButton had children (which is unlikely),
349 the style sheet would have no impact on them.
350
351 Style sheet cascading is a complex topic. Refer to the
352 \l{http://www.w3.org/TR/CSS2/cascade.html#cascade}{CSS2
353 Specification} for the gory details. Be aware that Qt currently
354 doesn't implement \c{!important}.
355
356 \section1 Inheritance
357
358 In classic CSS, when font and color of an item is not explicitly set,
359 it gets automatically inherited from the parent. By default, when using
360 Qt Style Sheets, a widget does \b{not} automatically inherit its font
361 and color setting from its parent widget.
362
363 For example, consider a QPushButton inside a QGroupBox:
364
365 \snippet code/doc_src_stylesheet.cpp 24
366
367 The QPushButton does not have an explicit color set. Hence, instead
368 of inheriting color of its parent QGroupBox, it has the system color.
369 If we want to set the color on a QGroupBox and its children,
370 we can write:
371
372 \snippet code/doc_src_stylesheet.cpp 25
373
374 In contrast, setting a font and palette using QWidget::setFont() and
375 QWidget::setPalette() propagates to child widgets.
376
377 If you would prefer that the font and palette propagate to child widgets,
378 you can set the Qt::AA_UseStyleSheetPropagationInWidgetStyles flag, like
379 this:
380
381 Usage:
382 \snippet code/doc_src_stylesheet.cpp 96
383
384 When the widget-style font and palette propagation is enabled, font and
385 palette changes made through Qt Style Sheets will behave as if the user
386 had manually called the corresponding QWidget::setPalette() and
387 QWidget::setFont() methods on all of the QWidgets targeted by the style
388 sheet.
389
390 \list
391 \li Changes made by a style sheet are propagated.
392 They are pushed to all widgets matching the style sheet once, at the time
393 the change is made.
394 \li Changes made by calling QWidget::setPalette() or QWidget::setFont() are
395 inherited.
396 They are inherited by all existing and future children, where the respective
397 brush or font hasn't been explicitly set.
398 \endlist
399
400 \section1 Widgets Inside C++ Namespaces
401
402 The Type Selector can be used to style widgets of a particular type. For
403 example,
404
405 \snippet code/doc_src_stylesheet.cpp 26
406
407 Qt Style Sheet uses QObject::className() of the widget to determine
408 when to apply the Type Selector. When custom widgets are inside namespaces,
409 the QObject::className() returns <namespace>::<classname>. This conflicts
410 with the syntax for \l{Sub-Controls}. To overcome this problem,
411 when using the Type Selector for widgets inside namespaces, we must
412 replace the \c{::} with \c{--}. For example,
413
414 \snippet code/doc_src_stylesheet.cpp 27
415
416 \section1 Setting QObject Properties
417
418 From 4.3 and above, any designable Q_PROPERTY
419 can be set using the qproperty-<property name> syntax.
420
421 For example,
422 \snippet code/doc_src_stylesheet.qdoc 28
423
424 If the property references an enum declared with Q_ENUM, you should
425 reference its constants by name, not their numeric value.
426
427 \note Use the qproperty syntax with care, as it modifies the
428 widget that is being painted. Also, the qproperty syntax is evaluated only
429 once, which is when the widget is polished by the style. This means that any
430 attempt to use them in pseudo-states such as QPushButton:hover, will not work.
431*/