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.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.html
6 \title Qt Style Sheets
7 \brief How to use style sheets to customize the appearance of widgets.
8
9 \ingroup frameworks-technologies
10 \ingroup qt-basic-concepts
11 \ingroup qt-gui-concepts
12
13 \previouspage {Styles and Style Aware Widgets}{Styles}
14 \nextpage The Style Sheet Syntax
15
16 \keyword style sheet
17 \keyword stylesheet
18
19 Qt Style Sheets let you customize the appearance of widgets with textual
20 rules, in addition to what is possible by subclassing \l QStyle. The
21 concepts, terminology, and syntax of Qt Style Sheets are heavily inspired by
22 HTML \l{http://www.w3.org/Style/CSS/}{Cascading Style Sheets (CSS)} but
23 adapted to the world of widgets.
24
25 Style sheets are a tool for prototyping and for small, local adjustments;
26 for the production look of an application, implement a \l QStyle or use
27 \l{Qt Labs StyleKit}{StyleKit}. \l{Styling Approaches for Qt Widgets}
28 explains the costs of style sheets and compares the approaches.
29
30 Topics:
31
32 \list
33 \li \l{Overview}
34 \li \l{The Style Sheet Syntax}
35 \li \l{Qt Widgets Designer Integration}
36 \li \l{Customizing Qt Widgets Using Style Sheets}
37 \li \l{Qt Style Sheets Reference}
38 \li \l{Qt Style Sheets Examples}
39 \endlist
40
41 \note If Qt Style Sheets are used on the same widget as functions that
42 set the appearance of widgets, such as \l QWidget::setFont() or
43 \l QTreeWidgetItem::setBackground(), style sheets will take precedence
44 if the settings conflict.
45
46 \target overview
47 \section1 Overview
48
49 Styles sheets are textual specifications that can be set on the
50 whole application using \l QApplication::setStyleSheet() or on a
51 specific widget (and its children) using
52 \l QWidget::setStyleSheet(). If several style sheets are set at
53 different levels, Qt derives the effective style sheet from all
54 of those that are set. This is called cascading.
55
56 Set the application style sheet once, before the first window is shown.
57 Changing the style sheet at runtime can lead to inconsistent results where
58 only some parts of the previous style sheet are reverted by the new style sheet.
59 Don't use this function to switch themes at runtime.
60
61 For example, the following style sheet specifies that all
62 \l{QLineEdit}s should use yellow as their background color, and
63 all \l{QCheckBox}es should use red as the text color:
64
65 \snippet code/doc_src_stylesheet.qdoc 0
66
67 For this kind of customization, style sheets are much more
68 powerful than QPalette. For example, it might be tempting to set
69 the QPalette::Button role to red for a QPushButton to obtain a
70 red push button. However, this wasn't guaranteed to work for all
71 styles, because style authors are restricted by the different
72 platforms' guidelines and (on Windows and \macos) by the
73 native theme engine.
74
75 Style sheets let you perform all kinds of customizations that are
76 difficult or impossible to perform using QPalette alone. If you
77 want yellow backgrounds for mandatory fields, red text for
78 potentially destructive push buttons, or fancy check boxes, style
79 sheets are the answer.
80
81 Style sheets are applied on top of the current \l{QStyle}{widget
82 style}, meaning that your applications will look as native as
83 possible, but any style sheet constraints will be taken into
84 consideration. Unlike palette fiddling, style sheets offer
85 guarantees: If you set the background color of a QPushButton to be
86 red, you can be assured that the button will have a red background
87 in all styles, on all platforms. In addition, \QD
88 provides style sheet integration, making it easy to view the effects
89 of a style sheet in different \l{QStyle}{widget styles}.
90
91 Style sheets can also give a prototype a distinctive look without
92 subclassing \l QStyle, for example, by specifying images for radio buttons and checkboxes.
93
94 When a style sheet is active, the \l QStyle returned by \l QWidget::style()
95 is a wrapper "style sheet" style, \e not the platform-specific style. The
96 wrapper style ensures that any active style sheet is respected and
97 otherwise forwards the drawing operations to the underlying,
98 platform-specific style (for example, the \c windows11 style on Windows, or
99 the \c macos style on \macos; see \l{QStyleFactory}).
100
101 \section1 Security Considerations
102
103 Style sheet text is parsed by Qt and can reference external resources: the
104 \c{url()} function can point at any local file or Qt resource, which Qt
105 will then load and render, for example as a background, border, or icon
106 image. Applications that construct a style sheet, in whole or in part,
107 from data that is not fully under their own control - such as a
108 runtime-selectable theme file, a path supplied via the \c{-stylesheet}
109 command-line option, or content coming from a plugin - should treat that
110 content as they would any other file path taken from an untrusted source.
111 A malicious style sheet can cause Qt to read and display the contents of
112 arbitrary local files that the application process has permission to
113 access.
114
115 Style sheets that are hardcoded in the application, or embedded in a
116 compiled-in Qt resource file, are not affected by this consideration,
117 since their content is fully controlled by the application itself.
118*/