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
styles-style-aware-widgets.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page style-aware-widgets.html
6
\title Writing Style-Aware Widgets
7
\brief How to paint a custom widget through the current style so that it
8
looks right in every style.
9
10
A style-aware widget conforms to the style in which it's drawn. Instead of
11
painting frames, buttons, and indicators itself, it asks the current style
12
to draw them, and it asks the style for the metrics it needs to lay out its
13
contents. The widget then looks native on every platform, follows the
14
application palette, and picks up a custom style or a style sheet like Qt's
15
own widgets do.
16
17
\section1 Build a style option
18
19
The style draws from the information in a \l QStyleOption, so the first step
20
in a paint event is to fill one out. Pick the option class that matches the
21
element you want to draw, call \l QStyleOption::initFrom() to set the state
22
flags, rectangle, palette, and font metrics that every widget shares, and
23
then set the members that are specific to your element.
24
25
Qt's widgets do this in a protected virtual \c initStyleOption() function,
26
so that subclasses can adjust the option. Follow the same pattern in your
27
widget.
28
29
\section1 Draw through the style
30
31
Draw the elements with the drawing functions of \l QStyle, passing the
32
option, a \l QPainter, and the widget itself:
33
34
\snippet styles/styles.cpp 2
35
\dots
36
\snippet styles/styles.cpp 3
37
38
\l QStylePainter combines a \l QStyle, a \l QPainter, and a \l QWidget. Its
39
drawing functions take the element and the option only, which makes the code
40
shorter:
41
42
\snippet styles/styles.cpp 5
43
\dots
44
\snippet styles/styles.cpp 7
45
46
\image paintsystem-stylepainter.png
47
{Diagram showing QStylePainter inherits from QPainter}
48
49
Use the palette in the option, or \l QWidget::palette(), for any color you
50
draw yourself, so that the widget follows the application palette and works
51
in both light and dark color schemes.
52
53
\section1 Ask the style for sizes
54
55
Base your \l{QWidget::}{sizeHint()} and \l{QWidget::}{minimumSizeHint()} on
56
the style. \l{QStyle::}{sizeFromContents()} turns the size of your contents
57
into the size of the widget for a given contents type, and
58
\l{QStyle::}{pixelMetric()} returns the style's frame widths, margins, and
59
indicator sizes. Use \l{QStyle::}{subElementRect()} to find out where the
60
style places the parts of an element, instead of computing the positions
61
yourself.
62
63
\section1 Report the widget's state
64
65
The style draws hover and focus effects only if the state flags are set. Set
66
the \l{Qt::}{WA_Hover} attribute on your widget to receive hover events, and
67
add \c State_MouseOver to the option while the mouse is over the element.
68
Set \c State_Sunken while the widget is pressed, and \c State_On,
69
\c State_Off, or \c State_NoChange for checkable elements.
70
\l{Widget Style Reference} lists the flags that each element expects.
71
72
\section1 Follow style and palette changes
73
74
When the application style or palette changes, the widget receives a
75
\l QEvent::StyleChange or \l QEvent::PaletteChange event in
76
\l{QWidget::}{changeEvent()}. Invalidate any cached metrics there and call
77
\l{QWidget::}{updateGeometry()} if your size hint depends on the style.
78
79
\section1 Support right-to-left layouts
80
81
In a right-to-left layout, the style mirrors asymmetric elements. Use
82
\l{QStyle::}{visualRect()} to convert a rectangle from logical to screen
83
coordinates and \l{QStyle::}{visualAlignment()} to mirror an alignment, and
84
pass the option's \c direction through to the style. Test with the
85
\c -reverse command-line option.
86
87
\sa QStyle, QStylePainter, QStyleOption, {How a Style Draws a Widget},
88
{Widget Style Reference}
89
*/
qtbase
src
widgets
doc
src
widgets-and-layouts
styles-style-aware-widgets.qdoc
Generated on
for Qt by
1.16.1