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-examples.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-examples.html
6
\previouspage Qt Style Sheets Reference
7
\title Qt Style Sheets Examples
8
9
We will now see a few examples to get started with using Qt Style Sheets.
10
11
\section1 Style Sheet Usage
12
13
\section2 Customizing the Foreground and Background Colors
14
15
Let's start by setting yellow as the background color of all
16
\l{QLineEdit}s in an application. This could be achieved like
17
this:
18
19
\snippet code/doc_src_stylesheet.cpp 88
20
21
If we want the property to apply only to the \l{QLineEdit}s that are
22
children (or grandchildren or grand-grandchildren) of a specific dialog,
23
we would rather do this:
24
25
\snippet code/doc_src_stylesheet.cpp 89
26
27
If we want the property to apply only to one specific QLineEdit,
28
we can give it a name using QObject::setObjectName() and use an
29
ID Selector to refer to it:
30
31
\snippet code/doc_src_stylesheet.cpp 90
32
33
Alternatively, we can set the
34
\l{Qt Style Sheets Reference#background-prop}{background-color} property directly on the
35
QLineEdit, omitting the selector:
36
37
\snippet code/doc_src_stylesheet.cpp 91
38
39
To ensure a good contrast, we should also specify a suitable
40
color for the text:
41
42
\snippet code/doc_src_stylesheet.cpp 92
43
44
It might be a good idea to change the colors used for selected
45
text as well:
46
47
\snippet code/doc_src_stylesheet.cpp 93
48
49
50
\section2 Customizing Using Dynamic Properties
51
52
There are many situations where we need to present a form that
53
has mandatory fields. To indicate to the user that the field is
54
mandatory, one effective (albeit esthetically dubious) solution
55
is to use yellow as the background color for those fields. It
56
turns out this is very easy to implement using Qt Style Sheets.
57
First, we would use the following application-wide style sheet:
58
59
\snippet code/doc_src_stylesheet.qdoc 94
60
61
This means that every widget whose \c mandatoryField Qt property
62
is set to true would have a yellow background.
63
64
Then, for each mandatory field widget, we would simply create a
65
\c mandatoryField property on the fly and set it to true. For
66
example:
67
68
\snippet code/doc_src_stylesheet.cpp 95
69
70
\section2 Customizing a QPushButton Using the Box Model
71
72
This time, we will show how to create a red QPushButton. This
73
QPushButton would presumably be connected to a very destructive
74
piece of code.
75
76
First, we are tempted to use this style sheet:
77
78
\snippet code/doc_src_stylesheet.qdoc 96
79
80
However, the result is a boring, flat button with no borders:
81
82
\image stylesheet-redbutton1.png {Flat red button}
83
84
What happened is this:
85
86
\list
87
\li We have made a request that cannot be satisfied using the
88
native styles alone (for example, the native Windows theme engine
89
doesn't let us specify the background color of a button).
90
\li Therefore, the button is rendered using style sheets.
91
\li We haven't specified any values for
92
\l{Qt Style Sheets Reference#border-width-prop}{border-width} and
93
\l{Qt Style Sheets Reference#border-style-prop}{border-style}, so by default we obtain
94
a 0-pixel wide border of style \c none.
95
\endlist
96
97
Let's improve the situation by specifying a border:
98
99
\snippet code/doc_src_stylesheet.qdoc 97
100
101
\image stylesheet-redbutton2.png {Red button with a beige border}
102
103
Things look already a lot better. But the button looks a bit
104
cramped. Let's specify some spacing between the border and the
105
text using the \l{Qt Style Sheets Reference#padding-prop}{padding}. Additionally, we will
106
enforce a minimum width, round the corners, and specify a larger
107
font to make the button look nicer:
108
109
\snippet code/doc_src_stylesheet.qdoc 98
110
111
\image stylesheet-redbutton3.png
112
{Red button with a round beige border and big, bold text}
113
114
The only issue remaining is that the button doesn't react when we
115
press it. We can fix this by specifying a slightly different
116
background color and use a different border style.
117
118
\snippet code/doc_src_stylesheet.qdoc 99
119
120
\section2 Customizing the QPushButton's Menu Indicator Sub-Control
121
122
Subcontrols give access to the sub-elements of a widget. For
123
example, a QPushButton associated with a menu (using
124
QPushButton::setMenu()) has a menu indicator. Let's customize
125
the menu indicator for the red push button:
126
127
\snippet code/doc_src_stylesheet.qdoc 100
128
129
By default, the menu indicator is located at the bottom-right
130
corner of the padding rectangle. We can change this by specifying
131
\l{Qt Style Sheets Reference#subcontrol-position-prop}{subcontrol-position} and
132
\l{Qt Style Sheets Reference#subcontrol-origin-prop}{subcontrol-origin} to anchor the
133
indicator differently. We can also use \l{Qt Style Sheets Reference#top-prop}{top} and
134
\l{Qt Style Sheets Reference#left-prop}{left} to move the indicator by a few pixels. For
135
example:
136
137
\snippet code/doc_src_stylesheet.qdoc 101
138
139
This positions the \c myindicator.png to the center right of the
140
QPushButton's \l{Qt Style Sheets Reference#padding-prop}{padding} rectangle (see
141
\l{Qt Style Sheets Reference#subcontrol-origin-prop}{subcontrol-origin} for more
142
information).
143
144
\section2 Complex Selector Example
145
146
Since red seems to be our favorite color, let's make the text in
147
QLineEdit red by setting the following application-wide
148
stylesheet:
149
150
\snippet code/doc_src_stylesheet.qdoc 102
151
152
However, we would like to give a visual indication that a
153
QLineEdit is read-only by making it appear gray:
154
155
\snippet code/doc_src_stylesheet.qdoc 103
156
157
At some point, our design team comes with the requirement that
158
all \l{QLineEdit}s in the registration form (with the
159
\l{QObject::objectName}{object name} \c registrationDialog) to be
160
brown:
161
162
\snippet code/doc_src_stylesheet.qdoc 104
163
164
A few UI design meetings later, we decide that all our
165
\l{QDialog}s should have brown colored \l{QLineEdit}s:
166
167
\snippet code/doc_src_stylesheet.qdoc 105
168
169
Quiz: What happens if we have a read-only QLineEdit in a QDialog?
170
[Hint: The \l{The Style Sheet Syntax#Conflict Resolution}{Conflict Resolution} section above explains
171
what happens in cases like this.]
172
173
\section1 Customizing Specific Widgets
174
175
This section provides examples to customize specific widgets using Style Sheets.
176
177
\section2 Customizing QAbstractScrollArea
178
179
The background of any QAbstractScrollArea (Item views, QTextEdit
180
and QTextBrowser) can be set using the background properties. For example,
181
to set a background-image that scrolls with the scroll bar:
182
\snippet code/doc_src_stylesheet.qdoc 106
183
184
If the background-image is to be fixed with the viewport:
185
\snippet code/doc_src_stylesheet.qdoc 107
186
187
\section2 Customizing QCheckBox
188
189
Styling of a QCheckBox is almost identical to styling a QRadioButton. The
190
main difference is that a tristate QCheckBox has an indeterminate state.
191
192
\snippet code/doc_src_stylesheet.qdoc 108
193
194
\section2 Customizing QComboBox
195
196
We will look at an example where the drop down button of a QComboBox
197
appears "merged" with the combo box frame.
198
199
\snippet code/doc_src_stylesheet.qdoc 109
200
201
The pop-up of the QComboBox is a QAbstractItemView and is styled using
202
the descendant selector:
203
\snippet code/doc_src_stylesheet.qdoc 110
204
205
\section2 Customizing QDockWidget
206
207
The title bar and the buttons of a QDockWidget can be customized as
208
follows:
209
210
\snippet code/doc_src_stylesheet.qdoc 111
211
212
If one desires to move the dock widget buttons to the left, the following
213
style sheet can be used:
214
215
\snippet code/doc_src_stylesheet.qdoc 112
216
217
\note To customize the separator (resize handle) of a QDockWidget,
218
use QMainWindow::separator.
219
220
\section2 Customizing QFrame
221
222
A QFrame is styled using the \l{The Box Model}.
223
224
\snippet code/doc_src_stylesheet.qdoc 113
225
226
\section2 Customizing QGroupBox
227
228
Let us look at an example that moves the QGroupBox's title to
229
the center.
230
231
\snippet code/doc_src_stylesheet.qdoc 114
232
233
For a checkable QGroupBox, use the \{#indicator-sub}{::indicator} subcontrol
234
and style it exactly like a QCheckBox (i.e)
235
236
\snippet code/doc_src_stylesheet.qdoc 115
237
238
\section2 Customizing QHeaderView
239
240
QHeaderView is customized as follows:
241
242
\snippet code/doc_src_stylesheet.qdoc 116
243
244
\section2 Customizing QLineEdit
245
246
The frame of a QLineEdit is styled using the \l{The Box Model}. To
247
create a line edit with rounded corners, we can set:
248
\snippet code/doc_src_stylesheet.qdoc 117
249
250
The password character of line edits that have QLineEdit::Password
251
echo mode can be set using:
252
\snippet code/doc_src_stylesheet.qdoc 118
253
254
The background of a read only QLineEdit can be modified as below:
255
\snippet code/doc_src_stylesheet.qdoc 119
256
257
\section2 Customizing QListView
258
259
The background color of alternating rows can be customized using the following
260
style sheet:
261
262
\snippet code/doc_src_stylesheet.qdoc 120
263
264
To provide a special background when you hover over items, we can use the
265
\l{item-sub}{::item} subcontrol. For example,
266
267
\snippet code/doc_src_stylesheet.qdoc 121
268
269
\section2 Customizing QMainWindow
270
271
The separator of a QMainWindow can be styled as follows:
272
273
\snippet code/doc_src_stylesheet.qdoc 122
274
275
\section2 Customizing QMenu
276
277
Individual items of a QMenu are styled using the 'item' subcontrol as
278
follows:
279
280
\snippet code/doc_src_stylesheet.qdoc 123
281
282
For a more advanced customization, use a style sheet as follows:
283
284
\snippet code/doc_src_stylesheet.qdoc 124
285
286
\section2 Customizing QMenuBar
287
288
QMenuBar is styled as follows:
289
290
\snippet code/doc_src_stylesheet.qdoc 125
291
292
\section2 Customizing QProgressBar
293
294
The QProgressBar's \l{stylesheet-reference.html#border-prop}{border},
295
\l{stylesheet-reference.html#chunk-sub}{chunk}, and
296
\l{stylesheet-reference.html#text-align-prop}{text-align} can be customized using
297
style sheets. However, if one property or sub-control is customized,
298
all the other properties or sub-controls must be customized as well.
299
300
\image progressBar-stylesheet.png {Progress bar showing 30%}
301
302
For example, we change the \l{stylesheet-reference.html#border-prop}
303
{border} to grey and the \l{stylesheet-reference.html#chunk-sub}{chunk}
304
to cerulean.
305
306
\snippet code/doc_src_stylesheet.qdoc 126
307
308
This leaves the \l{stylesheet-reference.html#text-align-prop}
309
{text-align}, which we customize by positioning the text in the center of
310
the progress bar.
311
312
\snippet code/doc_src_stylesheet.qdoc 127
313
314
A \l{stylesheet-reference.html#margin-prop}{margin} can be included to
315
obtain more visible chunks.
316
317
\image progressBar2-stylesheet.png {Notched progress bar}
318
319
In the screenshot above, we use a
320
\l{stylesheet-reference.html#margin-prop}{margin} of 0.5 pixels.
321
322
\snippet code/doc_src_stylesheet.qdoc 128
323
324
\section2 Customizing QPushButton
325
326
A QPushButton is styled as follows:
327
\snippet code/doc_src_stylesheet.qdoc 129
328
329
For a QPushButton with a menu, use the
330
\l{Qt Style Sheets Reference#menu-indicator-sub}{::menu-indicator}
331
subcontrol.
332
333
\snippet code/doc_src_stylesheet.qdoc 130
334
335
Checkable QPushButton have the \l{Qt Style Sheets Reference#checked-ps}
336
{:checked} pseudo state set.
337
338
\section2 Customizing QRadioButton
339
340
The indicator of a QRadioButton can be changed using:
341
\snippet code/doc_src_stylesheet.qdoc 131
342
343
\section2 Customizing QScrollBar
344
345
The QScrollBar can be styled using its subcontrols like
346
\l{stylesheet-reference.html#handle-sub}{handle},
347
\l{stylesheet-reference.html#add-line-sub}{add-line},
348
\l{stylesheet-reference.html#sub-line-sub}{sub-line}, and so on. Note that
349
if one property or sub-control is customized, all the other properties or
350
sub-controls must be customized as well.
351
352
\image stylesheet-scrollbar1.png {Green stylized scroll bar}
353
354
The scroll bar above has been styled in aquamarine with a solid grey
355
border.
356
357
\snippet code/doc_src_stylesheet.qdoc 132
358
359
\snippet code/doc_src_stylesheet.qdoc 133
360
361
\snippet code/doc_src_stylesheet.qdoc 134
362
363
The \l{stylesheet-reference.html#left-arrow-sub}{left-arrow} and
364
\l{stylesheet-reference.html#right-arrow-sub}{right-arrow} have a solid grey
365
border with a white background. As an alternative, you could also embed the
366
image of an arrow.
367
368
\snippet code/doc_src_stylesheet.qdoc 135
369
370
If you want the scroll buttons of the scroll bar to be placed together
371
(instead of the edges) like on \macos, you can use the following
372
stylesheet:
373
\snippet code/doc_src_stylesheet.qdoc 136
374
375
The scroll bar using the above stylesheet looks like this:
376
\image stylesheet-scrollbar2.png {Colorized scroll bar}
377
378
379
To customize a vertical scroll bar use a style sheet similar to the following:
380
\snippet code/doc_src_stylesheet.qdoc 137
381
382
\section2 Customizing QSizeGrip
383
384
QSizeGrip is usually styled by just setting an image.
385
386
\snippet code/doc_src_stylesheet.qdoc 138
387
388
\section2 Customizing QSlider
389
390
You can style horizontal slider as below:
391
\snippet code/doc_src_stylesheet.qdoc 139
392
393
If you want to change the color of the slider parts before and after the handle, you can use the add-page
394
and sub-page subcontrols. For example, for a vertical slider:
395
396
\snippet code/doc_src_stylesheet.qdoc 140
397
398
\section2 Customizing QSpinBox
399
400
QSpinBox can be completely customized as below (the style sheet has commentary inline):
401
402
\snippet code/doc_src_stylesheet.qdoc 141
403
404
405
\section2 Customizing QSplitter
406
407
A QSplitter derives from a QFrame and hence can be styled like a QFrame.
408
The grip or the handle is customized using the
409
\l{Qt Style Sheets Reference#handle-sub}{::handle} subcontrol.
410
411
\snippet code/doc_src_stylesheet.qdoc 142
412
413
\section2 Customizing QStatusBar
414
415
We can provide a background for the status bar and a border for items
416
inside the status bar as follows:
417
\snippet code/doc_src_stylesheet.qdoc 143
418
419
Note that widgets that have been added to the QStatusBar can be styled
420
using the descendant declaration (i.e)
421
\snippet code/doc_src_stylesheet.qdoc 144
422
423
\section2 Customizing QTabWidget and QTabBar
424
425
\image tabWidget-stylesheet1.png {Image of several tabs}
426
427
For the screenshot above, we need a stylesheet as follows:
428
429
\snippet code/doc_src_stylesheet.qdoc 145
430
431
Often we require the tabs to overlap to look like below:
432
\image tabWidget-stylesheet2.png {Image of overlapped tabs}
433
434
For a tab widget that looks like above, we make use of
435
\l{https://doc.qt.io/qt-5/stylesheet-customizing.html#the-box-model}
436
{negative margins}. Negative values draw the element closer to its
437
neighbors than it would be by default. The resulting stylesheet
438
looks like this:
439
440
\snippet code/doc_src_stylesheet.qdoc 146
441
442
To move the tab bar to the center (as below), we require the following stylesheet:
443
\image tabWidget-stylesheet3.png {Several tabs centered in the widget}
444
445
\snippet code/doc_src_stylesheet.qdoc 147
446
447
The tear indicator and the scroll buttons can be further customized as follows:
448
\snippet code/doc_src_stylesheet.qdoc 148
449
450
You can customize the close button as follows:
451
\snippet code/doc_src_stylesheet.qdoc 159
452
453
\section2 Customizing QTableView
454
455
Suppose we'd like our selected item in QTableView to have bubblegum pink
456
fade to white as its background.
457
458
\image tableWidget-stylesheet.png {Table view with custom style}
459
460
This is possible with the
461
\l{stylesheet-reference.html#selection-background-color-prop}
462
{selection-background-color} property and the syntax required is:
463
464
\snippet code/doc_src_stylesheet.qdoc 149
465
466
The corner widget can be customized using the following style sheet
467
468
\snippet code/doc_src_stylesheet.qdoc 150
469
470
The QTableView's checkbox indicator can also be customized. In the
471
following snippet the indicator \c background-color in unchecked state is
472
customized:
473
474
\snippet code/doc_src_stylesheet.qdoc 161
475
476
\section2 Customizing QToolBar
477
478
The background and the handle of a QToolBar is customized as below:
479
\snippet code/doc_src_stylesheet.qdoc 151
480
481
\section2 Customizing QToolBox
482
483
The tabs of the QToolBox are customized using the 'tab' subcontrol.
484
485
\snippet code/doc_src_stylesheet.qdoc 152
486
487
\section2 Customizing QToolButton
488
489
There are three types of QToolButtons.
490
\list
491
\li The QToolButton has no menu. In this case, the QToolButton is styled
492
exactly like QPushButton. See
493
\l{#Customizing QPushButton}{Customizing QPushButton} for an
494
example.
495
496
\li The QToolButton has a menu and has the QToolButton::popupMode set to
497
QToolButton::DelayedPopup or QToolButton::InstantPopup. In this case,
498
the QToolButton is styled exactly like a QPushButton with a menu.
499
See \l{#Customizing QPushButton}{Customizing QPushButton} for an
500
example of the usage of the menu-indicator pseudo state.
501
502
\li The QToolButton has its QToolButton::popupMode set to
503
QToolButton::MenuButtonPopup. In this case, we style it as follows:
504
\endlist
505
506
\snippet code/doc_src_stylesheet.qdoc 153
507
508
509
\section2 Customizing QToolTip
510
511
QToolTip is customized exactly like a QLabel. In addition, for platforms
512
that support it, the opacity property may be set to adjust the opacity.
513
514
For example,
515
\snippet code/doc_src_stylesheet.qdoc 154
516
517
\section2 Customizing QTreeView
518
519
The background color of alternating rows can be customized using the following
520
style sheet:
521
522
\snippet code/doc_src_stylesheet.qdoc 155
523
524
To provide a special background when you hover over items, we can use the
525
\l{item-sub}{::item} subcontrol. For example,
526
\snippet code/doc_src_stylesheet.qdoc 156
527
528
The branches of a QTreeView are styled using the
529
\l{Qt Style Sheets Reference#branch-sub}{::branch} subcontrol. The
530
following stylesheet color codes the various states when drawing
531
a branch.
532
533
\snippet code/doc_src_stylesheet.qdoc 157
534
535
Colorful, though it is, a more useful example can be made using the
536
following images:
537
538
\table
539
\row
540
\li \inlineimage stylesheet-vline.png {Vertical line}
541
\li \inlineimage stylesheet-branch-more.png {Junction line for lists}
542
\li \inlineimage stylesheet-branch-end.png {Line for terminating the list}
543
\li \inlineimage stylesheet-branch-closed.png {Arrow pointing right}
544
\li \inlineimage stylesheet-branch-open.png {Arrow pointing down}
545
\row
546
\li vline.png
547
\li branch-more.png
548
\li branch-end.png
549
\li branch-closed.png
550
\li branch-open.png
551
\endtable
552
553
\snippet code/doc_src_stylesheet.qdoc 158
554
555
The resulting tree view looks like this:
556
557
\image stylesheet-treeview.png {Tree view with styled branches}
558
559
\sa {Supported HTML Subset}, QStyle
560
561
562
\section1 Common Mistakes
563
564
This section lists some common mistakes when using stylesheets.
565
566
\section2 QPushButton and images
567
568
When styling a QPushButton, it is often desirable to use an image as the
569
button graphic. It is common to try the
570
\l{Qt Style Sheets Reference#background-image-prop}{background-image}
571
property,
572
but this has a number of drawbacks: For instance, the background will
573
often appear hidden behind the button decoration, because it is not
574
considered a background. In addition, if the button is resized, the
575
entire background will be stretched or tiled, which does not
576
always look good.
577
578
It is better to use the
579
\l{Qt Style Sheets Reference#border-image-prop}{border-image}
580
property, as it will always display the image,
581
regardless of the background (you can combine it with a background if it
582
has alpha values in it), and it has special settings to deal with button
583
resizing.
584
585
Consider the following snippet:
586
587
\snippet stylesheet/common-mistakes.cpp 1
588
589
This will produce a button looking like this:
590
591
\image stylesheet-border-image-normal.png {Button with a background}
592
593
The numbers after the url gives the top, right, bottom and left number of
594
pixels, respectively. These numbers correspond to the border and should not
595
stretch when the size changes.
596
Whenever you resize the button, the middle part of the image will stretch
597
in both directions, while the pixels specified in the stylesheet
598
will not. This makes the borders of the button look more natural, like
599
this:
600
601
\image stylesheet-border-image-stretched.png
602
{Button with specified border sizes}
603
\caption With borders
604
605
\image stylesheet-border-image-wrong.png
606
{Button without specified border sizes}
607
\caption Without borders
608
609
*/
qtbase
src
widgets
doc
src
widgets-and-layouts
stylesheet-examples.qdoc
Generated on
for Qt by
1.16.1