1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the documentation of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
44 \title Widget Appearance and Style
45 \brief Classes used for customizing UI appearance and style.
49 \page style-reference.html
50 \title Implementing Styles and Style Aware Widgets
51 \brief An overview of styles and the styling of widgets.
53 \ingroup frameworks-technologies
55 \previouspage Widget Classes
56 \contentspage Widgets and Layouts
57 \nextpage {Qt Style Sheets}{Style sheets}
59 Styles (classes that inherit QStyle) draw on behalf of widgets
60 and encapsulate the look and feel of a GUI. The QStyle class is
61 an abstract base class that encapsulates the look and feel of a
62 GUI. Qt's built-in widgets use it to perform nearly all of their
63 drawing, ensuring that they look exactly like the equivalent
66 Several styles are built into Qt (e.g., windows style and motif style).
67 Other styles are only available on specific platforms (such as
68 the windows XP style). Custom styles are made available as plugins
69 or by creating an instance of the style class in an application and
70 setting it with QApplication::setStyle().
72 To implement a new style, you inherit one of Qt's existing styles
73 - the one most resembling the style you want to create - and
74 reimplement a few virtual functions. This process is somewhat
75 involved, and we therefore provide this overview. We give a
76 step-by-step walkthrough of how to style individual Qt widgets.
77 We will examine the QStyle virtual functions, member variables,
80 The part of this document that does not concern the styling of
81 individual widgets is meant to be read sequentially because later
82 sections tend to depend on earlier ones. The description of the
83 widgets can be used for reference while implementing a style.
84 However, you may need to consult the Qt source code in some cases.
85 The sequence in the styling process should become clear after
86 reading this document, which will aid you in locating relevant code.
88 To develop style aware widgets (i.e., widgets that conform to
89 the style in which they are drawn), you need to draw them using the
90 current style. This document shows how widgets draw themselves
91 and which possibilities the style gives them.
95 \section1 Classes for Widget Styling
97 These classes are used to customize an application's appearance and
100 \annotatedlist appearance
102 \section1 The QStyle implementation
104 The API of QStyle contains functions that draw the widgets, static
105 helper functions to do common and difficult tasks (e.g.,
106 calculating the position of slider handles) and functions to do
107 the various calculations necessary while drawing (e.g., for the
108 widgets to calculate their size hints). The style also help some
109 widgets with the layout of their contents. In addition, it creates
110 a QPalette that contains \l{QBrush}es to draw with.
112 QStyle draws graphical elements; an element is a widget or a
113 widget part like a push button bevel, a window frame, or a scroll
114 bar. Most draw functions now take four arguments:
117 \o an enum value specifying which graphical element to draw
118 \o a QStyleOption specifying how and where to render that element
119 \o a QPainter that should be used to draw the element
120 \o a QWidget on which the drawing is performed (optional)
123 When a widget asks a style to draw an element, it provides the style
124 with a QStyleOption, which is a class that contains the information
125 necessary for drawing. Thanks to QStyleOption, it is possible to make
126 QStyle draw widgets without linking in any code for the widget. This
127 makes it possible to use \l{QStyle}'s draw functions on any paint
128 device. Ie you can draw a combobox on any widget, not just on a
131 The widget is passed as the last argument in case the style needs
132 it to perform special effects (such as animated default buttons on
133 Mac OS X), but it isn't mandatory.
135 We will in the course of this section look at the style elements,
136 the style options, and the functions of QStyle. Finally, we describe
137 how the palette is used.
139 Items in item views is drawn by \l{Delegate Classes}{delegates} in
140 Qt. The item view headers are still drawn by the style. Qt's
141 default delegate, QStyledItemDelegate, draws its items partially
142 through the current style; it draws the check box indicators and
143 calculate bounding rectangles for the elements of which the item
144 consists. In this document, we only describe how to implement a
145 QStyle subclass. If you wish to add support for other datatypes
146 than those supported by the QStyledItemDelegate, you need to
147 implement a custom delegate. Note that delegates must be set
148 programmatically for each individual widget (i.e., default
149 delegates cannot be provided as plugins).
151 \section2 The Style Elements
153 A style element is a graphical part of a GUI. A widget consists
154 of a hierarchy (or tree) of style elements. For instance, when a
155 style receives a request to draw a push button (from QPushButton,
156 for example), it draws a label (text and icon), a button bevel,
157 and a focus frame. The button bevel, in turn, consists of a frame
158 around the bevel and two other elements, which we will look at
159 later. Below is a conceptual illustration of the push button
160 element tree. We will see the actual tree for QPushButton when we
161 go through the individual widgets.
163 \image javastyle/conceptualpushbuttontree.png
165 Widgets are not necessarily drawn by asking the style to draw
166 only one element. Widgets can make several calls to the style to
167 draw different elements. An example is QTabWidget, which draws its
168 tabs and frame individually.
170 There are three element types: primitive elements, control
171 elements, and complex control elements. The elements are defined
172 by the \l{QStyle::}{ComplexControl}, \l{QStyle::}{ControlElement},
173 and \l{QStyle::}{PrimitiveElement} enums. The values of
174 each element enum has a prefix to identify their type: \c{CC_} for
175 complex elements, \c{CE_} for control elements, and \c{PE_} for
176 primitive elements. We will in the following three sections see what
177 defines the different elements and see examples of widgets that use
180 The QStyle class description contains a list of these elements and
181 their roles in styling widgets. We will see how they are used when
182 we style individual widgets.
184 \section3 Primitive Elements
186 Primitive elements are GUI elements that are common and often used
187 by several widgets. Examples of these are frames, button bevels,
188 and arrows for spin boxes, scroll bars, and combo boxes.
189 Primitive elements cannot exist on their own: they are always part
190 of a larger construct. They take no part in the interaction with
191 the user, but are passive decorations in the GUI.
193 \section3 Control Elements
195 A control element performs an action or displays information
196 to the user. Examples of control elements are push buttons, check
197 boxes, and header sections in tables and tree views. Control
198 elements are not necessarily complete widgets such as push
199 buttons, but can also be widget parts such as tab bar tabs and
200 scroll bar sliders. They differ from primitive elements in that
201 they are not passive, but fill a function in the interaction with
202 the user. Controls that consist of several elements often use the
203 style to calculate the bounding rectangles of the elements. The
204 available sub elements are defined by the \l{QStyle::}{SubElement}
205 enum. This enum is only used for calculating bounding rectangles,
206 and sub elements are as such not graphical elements to be drawn
207 like primitive, control, and complex elements.
209 \section3 Complex Control Elements
211 Complex control elements contain sub controls. Complex controls
212 behave differently depending on where the user handles them with
213 the mouse and which keyboard keys are pressed. This is dependent
214 on which sub control (if any) that the mouse is over or received a
215 mouse press. Examples of complex controls are scroll bars and
216 combo boxes. With a scroll bar, you can use the mouse to move the
217 slider and press the line up and line down buttons. The available
218 sub controls are defined by the \l{QStyle}{SubControl} enum.
220 In addition to drawing, the style needs to provide the widgets
221 with information on which sub control (if any) a mouse press was
222 made on. For instance, a QScrollBar needs to know if the user
223 pressed the slider, the slider groove, or one of the buttons.
225 Note that sub controls are not the same as the control elements
226 described in the previous section. You cannot use the style to
227 draw a sub control; the style will only calculate the bounding
228 rectangle in which the sub control should be drawn. It is common,
229 though, that complex elements use control and primitive elements
230 to draw their sub controls, which is an approach that is
231 frequently used by the built-in styles in Qt and also the Java
232 style. For instance, the Java style uses PE_IndicatorCheckBox to
233 draw the check box in group boxes (which is a sub control of
234 CC_GroupBox). Some sub controls have an equivalent control element,
235 e.g., the scroll bar slider (SC_SCrollBarSlider and
238 \section3 Other QStyle Tasks
240 The style elements and widgets, as mentioned, use the style to
241 calculate bounding rectangles of sub elements and sub controls,
242 and pixel metrics, which is a style dependent size in screen
243 pixels, for measures when drawing. The available rectangles and
244 pixel metrics are represented by three enums in QStyle:
245 \l{QStyle::}{SubElement}, \l{QStyle::}{SubControl}, and
246 \l{QStyle::}{PixelMetric}. Values of the enums can easily by
247 identified as they start with SE_, SC_ and PM_.
249 The style also contain a set of style hints, which is
250 represented as values in the \l{QStyle::}{StyleHint} enum. All
251 widgets do not have the same functionality and look in the
252 different styles. For instance, when the menu items in a menu do not
253 fit in a single column on the screen, some styles support
254 scrolling while others draw more than one column to fit all items.
256 A style usually has a set of standard images (such as a warning, a
257 question, and an error image) for message boxes, file dialogs,
258 etc. QStyle provides the \l{QStyle::}{StandardPixmap} enum. Its
259 values represent the standard images. Qt's widgets use these, so
260 when you implement a custom style you should supply the images
261 used by the style that is being implemented.
263 The style calculates the spacing between widgets in layouts. There
264 are two ways the style can handle these calculations. You can set
265 the PM_LayoutHorizontalSpacing and PM_LayoutVerticalSpacing, which
266 is the way the java style does it (through QCommonStyle).
267 Alternatively, you can implement QStyle::layoutSpacing() and
268 QStyle::layoutSpacingImplementation() if you need more control over
269 this part of the layout. In these functions you can calculate the
270 spacing based on control types (QSizePolicy::ControlType) for
271 different size policies (QSizePolicy::Policy) and also the style
272 option for the widget in question.
274 \section2 Style Options
276 The sub-classes of QStyleOption contain all information necessary
277 to style the individual elements. Style options are instantiated -
278 usually on the stack - and filled out by the caller of the QStyle
279 function. Depending on what is drawn the style will expect
280 different a different style option class. For example, the
281 QStyle::PE_FrameFocusRect element expects a QStyleOptionFocusRect
282 argument, and it's possible to create custom subclasses that a
283 custom style can use. The style options keep public variables
284 for performance reasons.
286 The widgets can be in a number of different states, which are
287 defined by the \l{QStyle::}{State} enum. Some of the state flags have
288 different meanings depending on the widget, but others are common
289 for all widgets like State_Disabled. It is QStyleOption that sets
290 the common states with QStyleOption::initFrom(); the rest of the
291 states are set by the individual widgets.
293 Most notably, the style options contain the palette and bounding
294 rectangles of the widgets to be drawn. Most widgets have
295 specialized style options. QPushButton and QCheckBox, for
296 instance, use QStyleOptionButton as style option, which contain
297 the text, icon, and the size of their icon. The exact contents of
298 all options are described when we go through individual widgets.
300 When reimplementing QStyle functions that take a
301 QStyleOption parameter, you often need to cast the
302 QStyleOption to a subclass (e.g., QStyleOptionFocusRect). For
303 safety, you can use qstyleoption_cast() to ensure that the
304 pointer type is correct. If the object isn't of the right type,
305 qstyleoption_cast() returns 0. For example:
307 \snippet doc/src/snippets/code/doc_src_qt4-styles.qdoc 0
309 The following code snippet illustrates how to use QStyle to
310 draw the focus rectangle from a custom widget's paintEvent():
312 \snippet doc/src/snippets/code/doc_src_qt4-styles.qdoc 1
314 The next example shows how to derive from an existing style to
315 customize the look of a graphical element:
317 \snippet doc/src/snippets/customstyle/customstyle.h 0
319 \snippet doc/src/snippets/customstyle/customstyle.cpp 2
320 \snippet doc/src/snippets/customstyle/customstyle.cpp 3
321 \snippet doc/src/snippets/customstyle/customstyle.cpp 4
323 \section2 QStyle Functions
325 The QStyle class defines three functions for drawing the primitive,
326 control, and complex elements:
327 \l{QStyle::}{drawPrimitive()},
328 \l{QStyle::}{drawControl()}, and
329 \l{QStyle::}{drawComplexControl()}. The functions takes the
330 following parameters:
333 \o the enum value of the element to draw
334 \o a QStyleOption which contains the information needed to
336 \o a QPainter with which to draw the element.
337 \o a pointer to a QWidget, typically the widget
338 that the element is painted on.
341 Not all widgets send a pointer to themselves. If the style
342 option sent to the function does not contain the information you
343 need, you should check the widget implementation to see if it
344 sends a pointer to itself.
346 The QStyle class also provides helper functions that are used
347 when drawing the elements. The \l{QStyle::}{drawItemText()}
348 function draws text within a specified rectangle and taking a
349 QPalette as a parameter. The \l{QStyle::}{drawItemPixmap()}
350 function helps to align a pixmap within a specified bounding
353 Other QStyle functions do various calculations for the
354 functions that draw. The widgets also use these functions for
355 calculating size hints and also for bounding rectangle
356 calculations if they draw several style elements themselves.
357 As with the functions that draw elements the helper functions
358 typically takes the same arguments.
361 \o The \l{QStyle::}{subElementRect()} function takes a
362 \l{QStyle::}{SubElement} enum value, and calculates a bounding
363 rectangle for a sub element. The style uses this function to
364 know where to draw the different parts of an element. This is
365 mainly done for reuse. If you create a new style, you can use
366 the same location of sub elements as the super class.
368 \o The \l{QStyle::}{subControlRect()} function is used to
369 calculate bounding rectangles for sub controls in complex
370 controls. When you implement a new style, you reimplement \c
371 subControlRect() and calculate the rectangles that are different
372 from the super class.
374 \o The \l{QStyle::}{pixelMetric()} function returns a pixel
375 metric, which is a style dependent size given in screen
376 pixels. It takes a value of the \l{QStyle::}{PixelMetric} enum
377 and returns the correct measure. Note that pixel metrics do
378 not necessarily have to be static measures, but can be
379 calculated with, for example, the style option.
381 \o The \l{QStyle::}{hitTestComplexControl()} function returns the
382 sub control that the mouse pointer is over in a complex control.
383 Usually, this is simply a matter of using
384 \l{QStyle::}{subControlRect()} to get the bounding rectangles of
385 the sub controls, and see which rectangle contains the position of
389 QStyle also have the functions \l{QStyle::}{polish()} and
390 \l{QStyle::}{unpolish()}. All widgets are sent to the \c polish()
391 function before being shown and to \c unpolish() when they
392 are hidden. You can use these functions to set attributes on the
393 widgets or do other work that is required by your style. For
394 instance, if you need to know when the mouse is hovering over the
395 widget, you need to set the \l{Qt::}{WA_Hover} widget attribute.
396 The State_MouseOver state flag will then be set in the widget's
399 QStyle has a few static helper functions that do some common and
400 difficult tasks. They can calculate the position of a slider
401 handle from the value of the slider and transform rectangles
402 and draw text considering reverse layouts; see the QStyle
403 class documentation for more details.
405 The usual approach when one reimplements QStyle virtual
406 functions is to do work on elements that are different from the
407 super class; for all other elements, you can simply use the super
408 class implementation.
410 \section2 The Palette
412 Each style provides a color - that is, QBrush - palette that
413 should be used for drawing the widgets. There is one set of colors
414 for the different widget states (QPalette::ColorGroup): active
415 (widgets in the window that has keyboard focus), inactive (widgets
416 used for other windows), and disabled (widgets that are set
417 disabled). The states can be found by querying the State_Active
418 and State_Enabled state flags. Each set contains color certain
419 roles given by the QPalette::ColorRole enum. The roles describe in
420 which situations the colors should be used (e.g., for painting
421 widget backgrounds, text, or buttons).
423 How the color roles are used is up to the style. For instance, if
424 the style uses gradients, one can use a palette color and make it
425 darker or lighter with QColor::darker() and QColor::lighter() to
426 create the gradient. In general, if you need a brush that is not
427 provided by the palette, you should try to derive it from one.
429 QPalette, which provides the palette, stores colors for
430 different widget states and color roles. The palette for a style
431 is returned by \l{QStyle::}{standardPalette()}. The standard
432 palette is not installed automatically when a new style is set
433 on the application (QApplication::setStyle()) or widget
434 (QWidget::setStyle()), so you must set the palette yourself
435 with (QApplication::setPalette()) or (QWidget::setPalette()).
437 It is not recommended to hard code colors as applications and
438 individual widgets can set their own palette and also use the
439 styles palette for drawing. Note that none of Qt's widgets set
440 their own palette. The java style does hard code some colors, but
441 its author looks past this in silence. Of course, it is not
442 intended that the style should look good with any palette.
444 \section2 Implementation Issues
446 When you implement styles, there are several issues to
447 consider. We will give some hints and advice on implementation
450 When implementing styles, it is necessary to look through the
451 code of the widgets and code of the base class and its ancestors.
452 This is because the widgets use the style differently, because the
453 implementation in the different styles virtual functions can
454 affect the state of the drawing (e.g., by altering the QPainter
455 state without restoring it and drawing some elements without using
456 the appropriate pixel metrics and sub elements).
458 It is recommended that the styles do not alter the proposed size
459 of widgets with the QStyle::sizeFromContents() function but let
460 the QCommonStyle implementation handle it. If changes need to be
461 made, you should try to keep them small; application development
462 may be difficult if the layout of widgets looks considerably
463 different in the various styles.
465 We recommend using the QPainter directly for drawing, i.e., not
466 use pixmaps or images. This makes it easier for the style conform
467 to the palette (although you can set your own color table on a
468 QImage with \l{QImage::}{setColorTable()}).
470 It is, naturally, possible to draw elements without using the
471 style to draw the sub elements as intended by Qt. This is
472 discouraged as custom widgets may depend on these sub elements to
473 be implemented correctly. The widget walkthrough shows how Qt
474 uses the sub elements.
478 We have implemented a style that resembles the Java default look
479 and feel (previously known as Metal). We have done this as it is
480 relatively simple to implement and we wanted to build a style for
481 this overview document. To keep it simple and not to extensive, we
482 have simplified the style somewhat, but Qt is perfectly able to
483 make an exact copy of the style. However, there are no concrete
484 plans to implement the style as a part of Qt.
486 In this section we will have a look at some implementation
487 issues. Finally, we will see a complete example on the styling of
488 a Java widget. We will continue to use the java style
489 throughout the document for examples and widget images. The
490 implementation itself is somewhat involved, and it is not
491 intended that you should read through it.
493 \section2 Design and Implementation
495 The first step in designing the style was to select the base
496 class. We chose to subclass QWindowsStyle. This class implements
497 most of the functionality we need other than performing the actual
498 drawing. Also, windows and java share layout of sub controls for
499 several of the complex controls (which reduces the amount of code
500 required considerably).
502 The style is implemented in one class. We have done this
503 because we find it convenient to keep all code in one file. Also,
504 it is an advantage with regards to optimization as we instantiate
505 less objects. We also keep the number of functions at a minimum by
506 using switches to identify which element to draw in the functions.
507 This results in large functions, but since we divide the code for
508 each element in the switches, the code should still be easy to
511 \section2 Limitations and Differences from Java
513 We have not fully implemented every element in the Java style.
514 This way, we have reduced the amount and complexity of the code.
515 In general, the style was intended as a practical example for
516 this style overview document, and not to be a part of Qt
519 Not all widgets have every state implemented. This goes for
520 states that are common, e.g., State_Disabled. Each state is,
521 however, implemented for at least one widget.
523 We have only implemented ticks below the slider. Flat push
524 buttons are also left out. We do not handle the case where the
525 title bars and dock window titles grows to small for their
526 contents, but simply draw sub controls over each other.
528 We have not tried to emulate the Java fonts. Java and Qt use very
529 different font engines, so we don't consider it worth the effort
530 as we only use the style as an example for this overview.
532 We have hardcoded the colors (we don't use the QPalette) for
533 the linear gradients, which are used, for example, for button
534 bevels, tool bars, and check boxes. This is because the Java
535 palette cannot produce these colors. Java does not change these
536 colors based on widget color group or role anyway (they are not
537 dependent on the palette), so it does not present a problem in any
540 It is Qt's widgets that are styled. Some widgets do not exist
541 at all in Java, e.g., QToolBox. Others contain elements that the
542 Java widgets don't. The tree widget is an example of the latter in
543 which Java's JTree does not have a header.
545 The style does not handle reverse layouts. We assume that the
546 layout direction is left to right. QWindowsStyle handles reverse
547 widgets; if we implemented reverse layouts, widgets that we change
548 the position of sub elements, or handle text alignment in labels
549 our selves would need to be updated.
551 \section2 Styling Java Check Boxes
553 As an example, we will examine the styling of check boxes in the
554 java style. We describe the complete process and print all code in
555 both the java style and Qt classes involved. In the rest of this
556 document, we will not examine the source code of the individual
557 widgets. Hopefully, this will give you an idea on how to search
558 through the code if you need to check specific implementation
559 details; most widgets follow the same structure as the check
560 boxes. We have edited the QCommonStyle code somewhat to remove
561 code that is not directly relevant for check box styling.
563 We start with a look at how QCheckBox builds it style option,
564 which is QStyleOptionButton for checkboxes:
566 \snippet doc/src/snippets/code/doc_src_styles.qdoc 0
568 First we let QStyleOption set up the option with the information
569 that is common for all widgets with \c initFrom(). We will look at
572 The down boolean is true when the user press the box down; this is
573 true whether the box is checked or not of the checkbox. The
574 State_NoChange state is set when we have a tristate checkbox and
575 it is partially checked. It has State_On if the box is checked and
576 State_Off if it is unchecked. State_MouseOver is set if the mouse
577 hovers over the checkbox and the widget has attribute Qt::WA_Hover
578 set - you set this in QStyle::polish(). In addition, the style
579 option also contains the text, icon, and icon size of the button.
581 \l{QStyleOption::}{initFrom()} sets up the style option with the
582 attributes that are common for all widgets. We print its
585 \snippet doc/src/snippets/code/doc_src_styles.qdoc 1
587 The State_Enabled is set when the widget is enabled. When the
588 widget has focus the State_HasFocus flag is set. Equally, the
589 State_Active flag is set when the widget is a child of the active
590 window. The State_MouseOver will only be set if the widget has
591 the WA_HoverEnabled windows flag set. Notice that keypad
592 navigation must be enabled in Qt for the State_HasEditFocus to
593 be included; it is not included by default.
595 In addition to setting state flags the QStyleOption contains
596 other information about the widget: \c direction is the layout
597 direction of the layout, \c rect is the bounding rectangle of the
598 widget (the area in which to draw), \c palette is the QPalette
599 that should be used for drawing the widget, and \c fontMetrics is
600 the metrics of the font that is used by the widget.
602 We give an image of a checkbox and the style option to match
605 \image javastyle/checkboxexample.png A java style checkbox
607 The above checkbox will have the following state flags in its
636 \o State_KeyboardFocusChange
643 The QCheckBox paints itself in QWidget::paintEvent() with
644 style option \c opt and QStylePainter \c p. The QStylePainter
645 class is a convenience class to draw style elements. Most
646 notably, it wraps the methods in QStyle used for painting. The
647 QCheckBox draws itself as follows:
649 \snippet doc/src/snippets/code/doc_src_styles.qdoc 2
651 QCommonStyle handles the CE_CheckBox element. The QCheckBox
652 has two sub elements: SE_CheckBoxIndicator (the checked indicator)
653 and SE_CheckBoxContents (the contents, which is used for the
654 checkbox label). QCommonStyle also implements these sub element
655 bounding rectangles. We have a look at the QCommonStyle code:
657 \snippet doc/src/snippets/code/doc_src_styles.qdoc 3
659 As can be seen from the code extract, the common style gets
660 the bounding rectangles of the two sub elements of
661 CE_CheckBox, and then draws them. If the checkbox has focus,
662 the focus frame is also drawn.
664 The java style draws CE_CheckBoxIndicator, while QCommonStyle
665 handles CE_CheckboxLabel. We will examine each implementation and
666 start with CE_CheckBoxLabel:
668 \snippet doc/src/snippets/code/doc_src_styles.qdoc 4
670 \l{QStyle::}{visualAlignment()} adjusts the alignment of text
671 according to the layout direction. We then draw an icon if it
672 exists, and adjust the space left for the text.
673 \l{QStyle::}{drawItemText()} draws the text taking alignment,
674 layout direction, and the mnemonic into account. It also uses the
675 palette to draw the text in the right color.
677 The drawing of labels often get somewhat involved. Luckily, it
678 can usually be handled by the base class. The java style
679 implements its own push button label since Java-contrary to
680 windows-center button contents also when the button has an icon.
681 You can examine that implementation if you need an example of
682 reimplementing label drawing.
684 We take a look at the java implementation
685 of CE_CheckBoxIndicator in \c drawControl():
687 \snippet doc/src/snippets/javastyle.cpp 0
689 We first save the state of the painter. This is not always
690 necessary but in this case the QWindowsStyle needs the painter in
691 the same state as it was when PE_IndicatorCheckBox was called (We
692 could also set the state with function calls, of course). We then
693 use \c drawButtonBackground() to draw the background of the check
694 box indicator. This is a helper function that draws the background
695 and also the frame of push buttons and check boxes. We take a look
696 at that function below. We then check if the mouse is hovering
697 over the checkbox. If it is, we draw the frame java checkboxes
698 have when the box is not pressed down and the mouse is over it.
699 You may note that java does not handle tristate boxes, so we have
702 Here we use a png image for our indicator. We could also check
703 here if the widget is disabled. We would then have to use
704 another image with the indicator in the disabled color.
706 \snippet doc/src/snippets/javastyle.cpp 1
708 We have seen how check boxes are styled in the java style from the
709 widget gets a paint request to the style is finished painting. To
710 learn in detail how each widget is painted, you need to go through
711 the code step-by-step as we have done here. However, it is
712 usually enough to know which style elements the widgets draw. The
713 widget builds a style option and calls on the style one or more
714 times to draw the style elements of which it consists. Usually,
715 it is also sufficient to know the states a widget can be in and the
716 other contents of the style option, i.e., what we list in the next
719 \section1 Widget Walkthrough
721 In this section, we will examine how most of Qt's widgets are
722 styled. Hopefully, this will save you some time and effort while
723 developing your own styles and widgets. You will not find
724 information here that is not attainable elsewhere (i.e., by
725 examining the source code or the class descriptions for the style
728 We mostly use java style widgets as examples. The java style does not
729 draw every element in the element trees. This is because they are
730 not visible for that widget in the java style. We still make sure
731 that all elements are implemented in a way that conforms with the
732 java style as custom widgets might need them (this does not
733 exclude leaving implementations to QWindowsStyle though).
735 The following is given for each widget:
738 \o A table with the members (variables, etc.) of its style option.
739 \o A table over the state flags (QStyle::StateFlag) that
740 can be set on the widget and when the states are set.
741 \o Its element tree (see section \l{The Style Elements}).
742 \o An image of the widget in which the elements are outlined.
743 \omit This is not written yet - probably never will be
745 \o List of style hints that should be checked for the
747 \o List of standard pixmaps that could be used by the
752 The element tree contains the primitive, control, and complex
753 style elements. By doing a top-down traversal of the element tree,
754 you get the sequence in which the elements should be drawn. In the
755 nodes, we have written the sub element rectangles, sub control
756 elements, and pixel metrics that should be considered when drawing
757 the element of the node.
759 Our approach on styling center on the drawing of the widgets. The
760 calculations of sub elements rectangles, sub controls, and pixel
761 metrics used \bold during drawing is only listed as contents in
762 the element trees. Note that there are rectangles and pixel
763 metrics that are only used by widgets. This leaves these
764 calculations untreated in the walkthrough. For instance, the
765 \l{QStyle::}{subControlRect()} and
766 \l{QStyle::}{sizeFromContents()} functions often call
767 \l{QStyle::}{subElementRect()} to calculate their bounding
768 rectangles. We could draw trees for this as well. However, how
769 these calculations are done is completely up to the individual
770 styles, and they do not have to follow a specific structure (Qt
771 does not impose a specific structure). You should still make sure
772 that you use the appropriate pixel metrics, though. To limit the
773 size of the document, we have therefore chosen not to include
774 trees or describe the calculations made by the Java (or any other)
777 You may be confused about how the different pixel metrics, sub
778 element rectangles, and sub control rectangles should be used when
779 examining the trees. If you are in doubt after reading the QStyle
780 enum descriptions, we suggest that you examine the QCommonStyle
781 and QWindowsStyle implementations.
783 Some of the bounding rectangles that we outline in the widget
784 images are equal. Reasons for this are that some elements draw
785 backgrounds while others draw frames and labels. If in doubt,
786 check the description of each element in QStyle. Also, some
787 elements are there to layout, i.e., decide where to draw, other
790 \section2 Common Widget Properties
792 Some states and variables are common for all widgets. These are
793 set with QStyleOption::initFrom(). Not all elements use this function;
794 it is the widgets that create the style options, and for some
795 elements the information from \l{QStyleOption::}{initFrom()} is not
798 A table with the common states follows:
806 \o Set if the widget is not disabled (see
807 QWidget::setEnabled())
810 \o Set if the widget has focus (see
813 \o State_KeyobordFocusChange
814 \o Set when the user changes focus with the keyboard
815 (see Qt::WA_KeyboardFocusChange)
818 \o Set if the mouse cursor is over the widget
821 \o Set if the widget is a child of the active window.
823 \o State_HasEditFocus
824 \o Set if the widget has the edit focus
827 The other common members for widgets are:
835 \o The bounding rectangle of the element to draw. This
836 is set to the widget bounding rectangle
840 \o The layout direction; a value of the
841 Qt::LayoutDirection enum.
844 \o The QPalette to use when drawing the element. This
845 is set to the widgets palette (QWidget::palette()).
848 \o The QFontMetrics to use when drawing text on the
852 The complex style options (classes that inherit
853 QStyleOptionComplex) used for complex style elements share two
854 variables: \l{QStyleOptionComplex::}{subControls} and
855 \l{QStyleOptionComplex::}{activeSubControls}. Both variables are
856 an OR'ed combination of QStyle::SubControl enum values. They
857 indicate which sub controls the complex control consists of and
858 which of these controls are currently active.
860 As mentioned, the style calculates the size of the widgets
861 contents, which the widgets calculate their size hints from. In
862 addition, complex controls also use the style to test which
863 sub-controls the mouse is over.
865 \section2 Widget Reference
867 Without further delay, we present the widget walkthrough; each
868 widget has its own sub-section.
870 \section3 Push Buttons
872 The style structure for push buttons is shown below. By doing a
873 top-down traversal of the tree, you get the sequence in which the
874 elements should be drawn.
876 \image javastyle/pushbutton.png The style structure for push buttons
878 The layout of the buttons, with regard element bounds, varies from
879 style to style. This makes it difficult to show conceptual images
880 of this. Also, elements may - even be intended to - have the same
881 bounds; the PE_PushButtonBevel, for instance, is used in
882 QCommonStyle to draw the elements that contains it:
883 PE_FrameDefaultButton, PE_FrameButtonBevel, and
884 PE_PanelButtonCommand, all of which have the same bounds in common
885 and windows style. PE_PushButtonBevel is also responsible for
886 drawing the menu indicator (QCommonStyle draws
887 PE_IndicatorArrowDown).
889 An image of a push button in the java style that show the bounding
890 rectangles of the elements is given below. Colors are used to
891 separate the bounding rectangles in the image; they do not fill
892 any other purpose. This is also true for similar images for the
895 \image javastyle/button.png
897 The java style, as well as all other styles implemented in Qt,
898 does not use PE_FrameButtonBevel. It is usual that a button
899 with a PE_DefaultFrame adjusts the PE_PanelButtonCommand's
900 rectangle by PM_ButtonDefaultIndicator. The CE_PushButtonLabel
901 is found by adjusting the rect by PM_DefaultFrameWidth.
903 We will now examine the style option for push
904 buttons - QStyleOptionButton. A table for the states that
905 QPushButton can set on the style option follows:
913 \o Button is down or menu is pressed shown
919 \o Button is not flat and not pressed down
922 Other members of QStyleOptionButton is:
930 \o Flags of the QStyleOptionButton::ButtonFeatures enum,
931 which describes various button properties (see enum)
934 \o The buttons QIcon (if any)
937 \o The QSize of the icon
940 \o a QString with the buttons text
943 \section3 Check and Radio Buttons
945 The structures for radio and check buttons are identical.
946 We show the structure using QCheckBox element and pixel
949 \image javastyle/checkbox.png
951 QStyleOptionButton is used as the style option for both check
952 and radio buttons. We first give a table of the states that
953 can be set in the option:
961 \o The box is pressed down
964 \o The box is partially checked (for tristate
968 \o The box is checked
971 \o The box is unchecked
974 See \l{Push Buttons} for a table over other members in the
975 QStyleOptionButtonClass.
979 In Qt, QTabBar uses the style to draw its tabs. Tabs exist either
980 in a QTabWidget, which contains a QTabBar, or as a separate bar.
981 If the bar is not part of a tab widget, it draws its own base.
983 QTabBar lays out the tabs, so the style does not have control over
984 tab placement. However, while laying out its tabs, the bar asks
985 the style for PM_TabBarTabHSpace and PM_TabBarTabVSpace, which is
986 extra width and height over the minimum size of the tab bar tab
987 label (icon and text). The style can also further influence the
988 tab size before it is laid out, as the tab bar asks for
989 CT_TabBarTab. The bounding rectangle of the bar is decided by the
990 tab widget when it is part of the widget (still considering
993 The tab bar is responsible for drawing the buttons that appear on
994 the tab bar when all tabs do not fit. Their placement is not
995 controlled by the style, but the buttons are \l{QToolButton}s
996 and are therefore drawn by the style.
998 Here is the style structure for QTabWidget and QTabBar:
1000 \image javastyle/tab.png
1002 The dotted lines indicate that the QTabWidget contains a tab bar,
1003 but does not draw it itself, that QTabBar only draws its base line
1004 when not part of a tab widget, and that the tab bar keeps two tool
1005 buttons that scroll the bar when all tabs do not fit; see \l{Tool
1006 Buttons} for their element tree. Also note that since the buttons
1007 are children of the tab bar, they are drawn after the bar. The
1008 tabs bounding rectangles overlap the base by PM_TabBarBaseOverlap.
1010 Here is a tab widget in the java style:
1012 \image javastyle/tabwidget.png
1014 In the java style (and also windows), the tab bar shape and label
1015 have the same bounding rectangle as CE_TabBarTab. Notice that the
1016 tabs overlap with the tab widget frame. The base of the tab bar
1017 (if drawn) is the area where the tabs and frame overlap.
1019 The style option for tabs (QStyleOptionTab) contains the necessary
1020 information for drawing tabs. The option contains the position of
1021 the tab in the tab bar, the position of the selected tab, the
1022 shape of the tab, the text, and icon. After Qt 4.1 the option
1023 should be cast to a QStyleOptionTabV2, which also contains the
1026 As the java style tabs don't overlap, we also present an image of
1027 a tab widget in the windows style. Note that if you want the tabs
1028 to overlap horizontally, you do that when drawing the tabs in
1029 CE_TabBarTabShape; the tabs bounding rectangles will not be
1030 altered by the tab bar. The tabs are drawn from left to right in a
1031 north tab bar shape, top to bottom in an east tab bar shape, etc.
1032 The selected tab is drawn last, so that it is easy to draw it over
1033 the other tabs (if it is to be bigger).
1035 \image javastyle/windowstabimage.png
1037 A table of the states a tab bar can set on its tabs follows:
1045 \o The tab is pressed on with the mouse.
1048 \o If it is the current tab.
1051 \o The tab bar has focus and the tab is selected
1054 Note that individual tabs may be disabled even if the tab bar
1055 is not. The tab will be active if the tab bar is active.
1057 Here follows a table of QStyleOptionTabV2's members:
1065 \o Is flags of the CornerWidget enum, which indicate
1066 if and which corner widgets the tab bar has.
1069 \o The QIcon of the tab
1072 \o The QSize of the icon
1075 \o A TabPosition enum value that indicates the tabs
1076 position on the bar relative to the other tabs.
1079 \o holds which row the tab is in
1082 \o A value of the SelectedPosition enum that indicates
1083 whether the selected tab is adjacent to or is the
1087 \o A value of the QTabBar::Shape enum indication
1088 whether the tab has rounded or triangular corners
1089 and the orientation of the tab.
1095 The frame for tab widgets use QStyleOptionTabWidgetFrame as
1096 style option. We list its members here. It does not have
1097 states set besides the common flags.
1104 \o leftCornerWidgetSize
1105 \o The QSize of the left corner widget (if any).
1107 \o rightCornerWidgetSize
1108 \o The QSize of the right corner widget (if any).
1111 \o holds the line with for drawing the panel.
1114 \o this value is currently always 0.
1117 \o The shape of the tabs on the tab bar.
1120 \o The QSize of the tab bar.
1123 \section3 Scroll Bars
1125 Here is the style structure for scrollBars:
1127 \image javastyle/scrollbar.png
1129 QScrollBar simply creates its style option and then draws
1130 CC_ScrollBar. Some styles draw the background of add page and sub
1131 page with PE_PanelButtonBevel and also use indicator arrows to
1132 draw the arrows in the nest and previous line indicators; we have
1133 not included these in the tree as their use is up to the
1134 individual style. The style's PM_MaximumDragDistance is the
1135 maximum distance in pixels the mouse can move from the bounds
1136 of the scroll bar and still move the handle.
1138 Here is an image of a scrollbar in the java style:
1140 \image javastyle/scrollbarimage.png
1142 You may notice that the scrollbar is slightly different from
1143 Java's as it has two line up indicators. We have done this to show
1144 how that you can have two separate bounding rectangles for a
1145 single sub control. The scroll bar is an example of a widget that
1146 is entirely implemented by the java style - neither QWindowsStyle
1147 nor QCommonStyle are involved in the drawing.
1149 We have a look at the different states a scroll bar can set on
1158 \o The scroll bar is horizontal
1161 The style option of QScrollBar is QStyleOptionSlider. Its
1162 members are listed in the following table. The option is used
1163 by all \l{QAbstractSlider}s; we only describe the members
1164 relevant for scroll bars here.
1172 \o the maximum value of the scroll bar
1175 \o the minimum value of the scroll bar
1178 \o the number of pixels between notches
1181 \o a value of the Qt::Orientation enum that specifies
1182 whether the scroll bar is vertical or horizontal
1185 \o the number to increase or decrease the sliders
1186 value (relative to the size of the slider and its value
1187 range) on page steps.
1190 \o the number to increase or decrease the sliders
1191 value on single (or line) steps
1194 \o The value of the slider
1197 \o the position of the slider handle. This is the same
1198 as \c sliderValue if the scroll bar is
1199 QAbstractSlider::tracking. If not, the scroll
1200 bar does not update its value before the mouse
1201 releases the handle.
1204 \o holds the direction in which the scroll bar
1205 increases its value. This is used instead of
1206 QStyleOption::direction for all abstract sliders.
1211 When calculating the sliders size hint, PM_SliderTickness and
1212 PM_SliderLength is queried from the style. As with scroll bars,
1213 the QSlider only lets the user move the handle if the mouse is
1214 within PM_MaximumDragDistance from the slider bounds. When it
1215 draws itself it creates the style option and calls \c
1216 drawComplexControl() with CC_Slider:
1218 \image javastyle/slider.png
1220 We also show a picture of a slider in the java style. We show
1221 the bounding rectangles of the sub elements as all drawing is done
1224 \image javastyle/sliderimage.png
1226 QSlider uses QStyleOptionSlider as all \l{QAbstractSlider}s do. We
1227 present a table with the members that affect QSlider:
1235 \o the maximum value of the slider
1238 \o the minimum value of the slider
1241 \o this is the number of pixels between each notch
1244 \o a Qt::Orientation enum value that gives whether the
1245 slider is vertical or horizontal.
1248 \o a number in slider value to increase or decrease
1252 \o the number to increase or decrease the sliders
1253 value on single (or line) steps.
1256 \o the value of the slider.
1259 \o the position of the slider given as a slider value.
1260 This will be equal to the \c sliderValue if the
1261 slider is \l{QAbstractSlider::}{tracking}; if
1262 not, the sliders value will not change until the handle is
1263 released with the mouse.
1266 \o this member is used instead of QStyleOption::direction
1267 for all abstract sliders.
1270 You should note that the slider does not use direction for
1271 reverse layouts; it uses \c upsideDown.
1273 \section3 Spin Boxes
1275 When QSpinBox paints itself it creates a QStyleOptionSpinBox and
1276 asks the style to draw CC_SpinBox. The edit field is a line
1277 edit that is a child of the spin box. The dimensions of the
1278 field is calculated by the style with SC_SpinBoxEditField.
1280 Here follows the style tree for spin boxes. It is not
1281 required that a style uses the button panel primitive to paint
1282 the indicator backgrounds. You can see an image below the tree
1283 showing the sub elements in QSpinBox in the java style.
1285 \image javastyle/spinbox.png
1287 \image javastyle/spinboximage.png
1289 The QStyleOptionSpinBox, which is the style option for spin
1290 boxes. It can set the following states on the spin box.:
1298 \o Is set if one of the sub controls CC_SpinUp or
1299 CC_SpinDown is pressed on with the mouse.
1302 The rest of the members in the spin boxes style options are:
1310 \o boolean that is true if the spin box is to draw a
1314 \o Value of the ButtonSymbols enum that decides the
1315 symbol on the up/down buttons.
1318 \o A value of the StepEnabled indication which of the
1319 spin box buttons are pressed down.
1324 The title bar complex control, CC_TitleBar, is used to draw
1325 the title bars of internal windows in QMdiArea. It typically
1326 consists of a window title and close, minimize, system menu, and
1327 maximize buttons. Some styles also provide buttons for shading
1328 the window, and a button for context sensitive help.
1330 The bar is drawn in CC_TitleBar without using any sub elements.
1331 How the individual styles draw their buttons is individual, but
1332 there are standard pixmaps for the buttons that the style should
1335 \image javastyle/titlebar.png
1337 In an image over a title bar in the java style, we show the
1338 bounding rectangles of the sub elements supported by the java style
1339 (all of which are drawn with standard pixmaps). It is usual to
1340 draw the button backgrounds using PE_PanelButtonTool, but it's no
1343 \image javastyle/titlebarimage.png
1345 The style option for title bars is QStyleOptionTitleBar. It's
1354 \o The title bars icon
1357 \o the text for the title bar's label
1360 \o flags of the Qt::WindowFlag enum. The window flags
1361 used by QMdiArea for window management.
1364 \o this is the QWidget::windowState() of the window
1365 that contains the title bar.
1370 A QComboBox uses the style to draw the button and label of
1371 non-editable boxes with CC_ComboBox and CE_ComboBoxLabel.
1373 The list that pops up when the user clicks on the combo box is
1374 drawn by a \l{Delegate Classes}{delegate}, which we do not cover
1375 in this overview. You can, however, use the style to control the
1376 list's size and position with the sub element
1377 SC_ComboBoxListBoxPopup. The style also decides where the edit
1378 field for editable boxes should be with SC_ComboBoxEditField; the
1379 field itself is a QLineEdit that is a child of the combo box.
1381 \image javastyle/combobox.png
1383 We show an image over a java style combo box in which we have
1384 outlined its sub elements and sub element rectangles:
1386 \image javastyle/comboboximage.png
1388 Java combo boxes do not use the focus rect; it changes its
1389 background color when it has focus. The SC_ComboBoxEdit field is
1390 used both by QComboBox to calculate the size of the edit field and
1391 the style for calculating the size of the combo box label.
1393 The style option for combo boxes is QStyleOptionComboBox. It
1394 can set the following states:
1402 \o The box is not editable and has focus
1405 \o SC_ComboBoxArrow is active
1408 \o The container (list) of the box is visible
1411 The style options other members are:
1419 \o the icon of the current (selected) item of the
1423 \o the text of the current item in the box.
1426 \o holds whether the combo box is editable or not
1429 \o holds whether the combo box has a frame or not
1432 \o the size of the current items icon.
1435 \o the bounding rectangle of the combo box's popup
1439 \section3 Group Boxes
1441 When calculating the size hint, QGroupBox fetches three pixel
1442 metrics from the style: PM_IndicatorWidth,
1443 PM_CheckBoxLabelSpacing, and PM_IndicatorHeight. QGroupBox has
1444 the following style element tree:
1446 \image javastyle/groupbox.png
1448 Qt does not impose restrictions on how the check box is drawn; the
1449 java style draws it with CE_IndicatorCheckBox. See \l{Check and
1450 Radio Buttons} for the complete tree.
1452 We also give an image of the widget with the sub controls and
1453 sub control rectangles drawn:
1455 \image javastyle/groupboximage.png
1457 The style option for group boxes are QStyleOptionGroupBox. The
1458 following states can be set on it:
1466 \o The check box is checked
1469 \o The checkbox is pressed down
1472 \o The check box is unchecked (or there is no check box)
1475 The remaining members of QStyleOptionGroupBox are:
1483 \o flags of the QStyleOptionFrameV2::FrameFeatures
1484 enum describing the frame of the group box.
1487 \o the line width with which to draw the panel. This
1491 \o the text of the group box.
1494 \o the alignment of the group box title
1497 \o the QColor of the text
1502 As the structure of splitters are simple and do not contain any
1503 sub elements, we do not include image of splitters. CE_Splitter
1504 does not use any other elements or metrics.
1506 For its style option, Splitters uses the base class QStyleOption.
1507 It can set the following state flags on it:
1515 \o Set if it is a horizontal splitter
1518 QSplitter does not use \l{QStyleOption::}{initFrom()} to set up its
1519 option; it sets the State_MouseOver and State_Disabled flags
1522 \section3 Progress Bar
1524 The CE_ProgressBar element is used by QProgressBar, and it is the
1525 only element used by this widget. We start with looking at the
1528 \image javastyle/progressbar.png
1530 Here is a progress bar in the windows style (the java style
1531 bounding rectangles are equal):
1533 \image javastyle/progressbarimage.png
1535 The style option for QProgressBar is QStyleOptionProgressBarV2.
1536 The bar does not set any state flags, but the other members of the
1545 \o The minimum value of the bar
1548 \o The maximum value of the bar
1551 \o The current value of the bar
1554 \o How the text is aligned in the label
1557 \o Whether the label is drawn
1563 \o Progress bars can be vertical or horizontal
1565 \o invertedAppearance
1566 \o The progress is inverted (i.e., right to left in a
1570 \o Boolean that if true, turns the label of vertical
1571 progress bars 90 degrees.
1574 \section3 Tool Buttons
1576 Tool buttons exist either independently or as part of tool bars.
1577 They are drawn equally either way. The QToolButton draws only one
1578 style element: CC_ToolButton.
1580 As you must be used to by now (at least if you have read this
1581 document sequentially), we have a tree of the widget's style
1584 \image javastyle/toolbutton.png
1586 Note that PE_FrameButtonTool and PE_IndicatorArrowDown are
1587 included in the tree as the java style draws them, but they can
1588 safely be omitted if you prefer it. The structure may also be
1589 different. QWindowsStyle, for instance, draws both
1590 PE_IndicatorButtonDropDown and PE_IndicatorArrowDown in
1593 We also have an image of a tool button where we have outlined
1594 the sub element bounding rectangles and sub controls.
1596 \image javastyle/toolbuttonimage.png
1598 Here is the states table for tool buttons:
1606 \o the tool button has the autoRise property set
1609 \o the button is not sunken (i.e., by being checked or
1610 pressed on with the mouse).
1613 \o the button is down
1616 \o the button is checkable and checked.
1619 QStyleOptionToolButton also contains the following members:
1627 \o a Qt::ArrowType enum value, which contains the
1628 direction of the buttons arrow (if an arrow is to
1629 be used in place of an icon)
1632 \o flags of the QStyleOptionToolButton::ButtonFeature
1633 enum describing if the button has an arrow, a menu,
1634 and/or has a popup-delay.
1637 \o the QFont of the buttons label
1640 \o the QIcon of the tool button
1643 \o the icon size of the button's icon
1646 \o the position of the button, as given by
1650 \o the text of the button
1653 \o a Qt::ToolButtonStyle enum value which decides
1654 whether the button shows the icon, the text, or both.
1659 Toolbars are part of the \l{QMainWindow}{main window framework}
1660 and cooperates with the QMainWindow to which it belongs while it
1661 builds its style option. A main window has 4 areas that toolbars
1662 can be placed in. They are positioned next to the four sides of
1663 the window (i.e., north, south, west, and east). Within each area
1664 there can be more than one line of toolbars; a line consists of
1665 toolbars with equal orientation (vertical or horizontal) placed
1668 \l{QToolbar}{QToolbar}s in Qt consists of three elements
1669 CE_ToolBar, PE_IndicatorToolBarHandle, and
1670 PE_IndicatorToolBarSeparator. It is QMainWindowLayout that
1671 calculates the bounding rectangles (i.e., position and size of the
1672 toolbars and their contents. The main window also uses the \c
1673 sizeHint() of the items in the toolbars when calculating the size
1676 Here is the element tree for QToolBar:
1678 \image javastyle/toolbar.png
1680 The dotted lines indicate that the QToolBar keeps an instance of
1681 QToolBarLayout and that QToolBarSeparators are kept by
1682 QToolBarLayout. When the toolbar is floating (i.e., has its own
1683 window) the PE_FrameMenu element is drawn, else QToolbar draws
1686 Here is an image of a toolbar in the java style:
1688 \image javastyle/toolbarimage.png
1690 QToolBarSaparator uses QStyleOption for their style option. It
1691 sets the State_horizontal flag if the toolbar they live in is
1692 horizontal. Other than that, they use \l{QStyleOption::}{initFrom()}.
1694 The style option for QToolBar is QStyleOptionToolBar. The only
1695 state flag set (besides the common flags) is State_Horizontal
1696 if the bar is horizontal (i.e., in the north or south toolbar area).
1697 The member variables of the style option are:
1705 \o Holds whether the bar is movable in a value of the
1706 ToolBarFeature, which is either Movable or None.
1709 \o The width of the tool bar frame.
1712 \o This variable is currently not used and is always
1716 \o The position of the toolbar line within the toolbar
1717 area to which it belongs.
1719 \o positionWithinLine
1720 \o The position of the toolbar within the toolbar line.
1723 \o The toolbar area in which the toolbar lives.
1728 Menus in Qt are implemented in QMenu. The QMenu keeps a list of
1729 action, which it draws as menu items. When QMenu receives paint
1730 events ,it calculates the size of each menu item and draws them
1731 individually with CE_MenuItem. (Menu items do not have a separate
1732 element for their label (contents), so all drawing is done in
1733 CE_MenuItem. The menu also draws the frame of the menu with
1734 PE_FrameMenu. It also draws CE_MenuScroller if the style supports
1735 scrolling. CE_MenuTearOff is drawn if the menu is to large for its
1738 In the style structure tree, we also include QMenu as it also does
1739 styling related work. The bounding rectangles of menu items are
1740 calculated for the menus size hint and when the menu is displayed
1743 \image javastyle/menu.png
1745 The CE_MenuScroller and CE_MenuTearOff elements are handled by
1746 QCommonStyle and are not shown unless the menu is to large to fit
1747 on the screen. PE_FrameMenu is only drawn for pop-up menus.
1749 QMenu calculates rectangles based on its actions and calls
1750 CE_MenuItem and CE_MenuScroller if the style supports that.
1752 It is also usual to use PE_IndicatorCheckBox (instead of using
1753 PE_IndicatorMenuCheckMark) and PE_IndicatorRadioButton for drawing
1754 checkable menu items; we have not included them in the style tree
1755 as this is optional and varies from style to style.
1757 \image javastyle/menuimage.png
1759 The style option for menu items is QStyleOptionMenuItem. The
1760 following tables describe its state flags and other members.
1768 \o The mouse is over the action and the action is not
1772 \o The mouse is pressed down on the menu item.
1775 \o Set if the menu item is a menu scroller and it scrolls
1785 \o A value of the \l{QStyleOptionMenuItem::}{CheckType} enum,
1786 which is either NotCheckable, Exclusive, or
1790 \o Boolean that is true if the menu item is checked.
1793 \o The QFont to use for the menu item's text.
1796 \o the QIcon of the menu item.
1799 \o The maximum width allowed for the icon
1801 \o menuHasChecableItem
1802 \o Boolean which is true if at least one item in the
1806 \o The type of the menu item. This a value of the
1807 \l{QStyleOptionMenuItem::}{MenuItemType}.
1810 \o The bounding rectangle for the QMenu that the menu
1814 \o This is the distance between the text of the menu
1815 item and the shortcut.
1818 \o The text of the menu item.
1821 The setup of the style option for CE_MenuTearOff and
1822 CE_MenuScroller also uses QStyleOptionMenuItem; they only set the
1823 \c menuRect variable in addition to the common settings with
1824 QStyleOption's \l{QStyleOption::}{initFrom()}.
1828 QMenuBar uses the style to draw each menu bar item and the empty
1829 area of the menu bar. The pull-down menus themselves are
1830 \l{QMenu}s (see \l{Menus}). The style element tree for the menu
1833 \image javastyle/menubar.png
1835 The panel and empty area is drawn after the menu items. The
1836 QPainter that the QMenuBar sends to the style has the bounding
1837 rectangles of the items clipped out (i.e., clip region), so you
1838 don't need to worry about drawing over the items. The pixel
1839 metrics in QMenuBar is used when the bounding rectangles of the
1840 menu bar items are calculated.
1842 \image javastyle/menubarimage.png
1844 QStyleOptionMenuItem is used for menu bar items. The members that
1845 are used by QMenuBar is described in the following table:
1853 \o the bounding rectangle of the entire menu bar to
1854 which the item belongs.
1857 \o the text of the item
1860 \o the icon of the menu item (it is not common that
1861 styles draw this icon)
1864 QStyleOptionMenuItem is also used for drawing CE_EmptyMenuBarArea.
1866 QStyleOptionFrame is used for drawing the panel frame The
1867 \l{QStyleOptionFrame::}{lineWidth} is set to PM_MenuBarPanelWidth.
1868 The \l{QStyleOptionFrame::}{midLineWidth} is currently always set
1871 \section3 Item View Headers
1873 It is the style that draws the headers of Qt's item views. The
1874 item views keeps the dimensions on individual sections. Also
1875 note that the delegates may use the style to paint decorations
1876 and frames around items. QItemDelegate, for instance, draws
1877 PE_FrameFocusRect and PE_IndicatorViewItemCheck.
1879 \image javastyle/header.png
1881 Here is a QTableWidget showing the bounding rects of a Java
1884 \image javastyle/headerimage.png
1886 The QHeaderView uses CT_HeaderSection, PM_HeaderMargin and
1887 PM_HeaderGripMargin for size and hit test calculations. The
1888 PM_HeaderMarkSize is currently not used by Qt. QTableView draws
1889 the button in the top-left corner (i.e., the area where the
1890 vertical and horizontal headers intersect) as a CE_Header.
1892 The style option for header views is QStyleOptionHeader. The view
1893 paints one header section at a time, so the data is for the
1894 section being drawn. Its contents are:
1902 \o the icon of the header (for section that is being
1906 \o the alignment (Qt::Alignment) of the icon in the header.
1909 \o a Qt::Orientation value deciding whether the header
1910 is the horizontal header above the view or the
1911 vertical header on the left.
1914 \o a QStyleOptionHeader::SectionPosition value
1915 giving the header section's position relative to
1919 \o holds the section that is being drawn.
1922 \o a QStyleOptionHeader::SelectedPosition value giving
1923 the selected section's position relative to the
1924 section that is being painted.
1927 \o a QStyleOptionHeader::SortIndicator value that
1928 describes the direction in which the section's sort
1929 indicator should be drawn.
1932 \o the text of the currently drawn section.
1935 \o the Qt::Alignment of the text within the
1939 \section3 Tree Branch Indicators
1941 The branch indicators in a tree view is drawn by the style with
1942 PE_IndicatorBranch. We think of indicators here as the indicators
1943 that describe the relationship of the nodes in the tree. The
1944 generic QStyleOption is sent to the style for drawing this
1945 elements. The various branch types are described by states. Since
1946 there are no specific style option, we simply present the states
1955 \o the node in the tree has a sibling (i.e., there is
1956 another node in the same column).
1959 \o this branch indicator has an item.
1962 \o the branch has children (i.e., a new sub-tree can
1963 be opened at the branch).
1966 \o the branch indicator has an opened sub-tree.
1969 The tree view (and tree widget) use the style to draw the branches
1970 (or nodes if you will) of the tree.
1972 QStyleOption is used as the style for PE_IndicatorBranch has state
1973 flags set depending on what type of branch it is.
1975 Since there is no tree structure for branch indicators, we only
1976 present an image of a tree in the java style. Each state is marked
1977 in the image with a rectangle in a specific color (i.e., these
1978 rectangles are not bounding rectangles). All combinations of
1979 states you must be aware of are represented in the image.
1981 \image javastyle/branchindicatorimage.png
1983 \section3 Tool Boxes
1985 PM_SmallIconSize for sizeHints.
1987 QToolBox is a container that keeps a collection of widgets. It has
1988 one tab for each widget and display one of them at a time. The
1989 tool box lays the components it displays (the tool box buttons
1990 and selected widget) in a QVBoxLayout. The style tree for tool
1991 boxes looks like this:
1993 \image javastyle/toolbox.png
1995 We show an image of a tool box in the Plastique style:
1997 \image javastyle/toolboximage.png
1999 All elements have the same bounding rectangles in the
2000 Plastique as well as the other Qt built-in styles.
2002 The style option for tool boxes is QStyleOptionToolBox. It
2003 contains the text and icon of the tool box contents. The only
2004 state set by QToolBox is State_Sunken, which is set when the user
2005 presses a tab down with the mouse. The rest of the
2006 QStyleOptionToolBox members are:
2014 \o the icon on the toolbox tab
2017 \o the text on the toolbox tab
2022 The size grip calculates its size hint with CT_SizeGrip. The pixel
2023 metric PM_SizeGripSize is currently unused by Qt. The element tree
2024 for and an image in the Plastique style of QSizeGrip follows:
2026 \image javastyle/sizegrip.png
2028 \image javastyle/sizegripimage.png
2030 We show the size grip in a \l{QMainWindow}'s bottom right
2033 The size grip style option, QStyleOptionSizeGrip, have one
2034 member except the common members from QStyleOption:
2042 \o a Qt::Corner value that describe which corner in a
2043 window (or equivalent) the grip is located.
2046 \section3 Rubber Band
2048 The \l{QRubberBand}'s style tree consists of two nodes.
2050 \image javastyle/rubberband.png
2052 We present an image of a Java style window being moved in a
2053 QMdiArea with a rubber band:
2055 \image javastyle/rubberbandimage.png
2057 The style option for rubber bands is QStyleOptionRubberBand.
2066 \o boolean that is true if the rubber band must be
2067 drawn in an opaque style (i.e., color)
2070 \o a QRubberBand::Shape enum value that holds the
2071 shape of the band (which is either a rectangle or a
2075 \section3 Dock Widgets
2077 When the dock widget lays out its contents it asks the style for
2078 these pixel metrics: PM_DockWidgetSeparatorExtent,
2079 PM_DockWidgetTitleBarButtonMargin, PM_DockWidgetFrameWidth, and
2080 PM_DockWidgetTitleMargin. It also calculates the bounding
2081 rectangles of the float and close buttons with
2082 SE_DockWidgetCloseButton and SE_DockWidgetFloatButton.
2084 \image javastyle/dockwidget.png
2086 The dotted lines indicate that the sender keeps instances of the
2087 recipient of the arrow (i.e., it is not a style element to draw).
2088 The dock widget only draws PE_frameDockWidget when it is detached
2089 from its main window (i.e., it is a top level window). If it is
2090 docked it draws the indicator dock widget resize handle. We show a
2091 dock widget in both docked and floating state in the plastique
2094 \image javastyle/dockwidgetimage.png
2096 The style option is QStyleOptionDockWidget:
2104 \o boolean that holds whether the dock window can be
2108 \o boolean that holds whether the dock window can
2109 float (i.e., detach from the main window in which
2113 \o boolean that holds whether the window is movable
2114 (i.e., can move to other dock widget areas)
2117 \o the title text of the dock window
2120 For the buttons, QStyleOptionButton is used (see \l{Tool Buttons}
2121 for content description). The dock widget resize handle has a