Additional Inherited Members

Detailed Description

The QStyle class is an abstract base class that encapsulates the look and feel of a GUI.

Qt contains a set of QStyle subclasses that emulate the styles of the different platforms supported by Qt (QWindowsStyle, QMacStyle, QMotifStyle, etc.). By default, these styles are built into the QtGui library. Styles can also be made available as plugins.

Qt's built-in widgets use QStyle to perform nearly all of their drawing, ensuring that they look exactly like the equivalent native widgets. The diagram below shows a QComboBox in eight different styles.

Topics:

Setting a Style

The style of the entire application can be set using the QApplication::setStyle() function. It can also be specified by the user of the application, using the -style command-line option:

./myapplication -style motif

If no style is specified, Qt will choose the most appropriate style for the user's platform or desktop environment.

A style can also be set on an individual widget using the QWidget::setStyle() function.

QStyle gets all the information it needs to render the graphical element from QStyleOption. The widget is passed as the last argument in case the style needs it to perform special effects (such as animated default buttons on Mac OS X), but it isn't mandatory. In fact, you can use QStyle to draw on any paint device, not just widgets, by setting the QPainter properly.

Creating a Custom Style

You can create a custom look and feel for your application by creating a custom style. There are two approaches to creating a custom style. In the static approach, you either choose an existing QStyle class, subclass it, and reimplement virtual functions to provide the custom behavior, or you create an entire QStyle class from scratch. In the dynamic approach, you modify the behavior of your system style at runtime. The static approach is described below. The dynamic approach is described in QProxyStyle.

The first step in the static approach is to pick one of the styles provided by Qt from which you will build your custom style. Your choice of QStyle class will depend on which style resembles your desired style the most. The most general class that you can use as a base is QCommonStyle (not QStyle). This is because Qt requires its styles to be QCommonStyles.

Depending on which parts of the base style you want to change, you must reimplement the functions that are used to draw those parts of the interface. To illustrate this, we will modify the look of the spin box arrows drawn by QWindowsStyle. The arrows are primitive elements that are drawn by the drawPrimitive() function, so we need to reimplement that function. We need the following class declaration:

Notice that we don't use the widget argument, except to pass it on to the QWindowStyle::drawPrimitive() function. As mentioned earlier, the information about what is to be drawn and how it should be drawn is specified by a QStyleOption object, so there is no need to ask the widget.

If you need to use the widget argument to obtain additional information, be careful to ensure that it isn't 0 and that it is of the correct type before using it. For example:

The documentation for the Styles example covers this topic in more detail.

Warning: Qt style sheets are currently not supported for custom QStyle subclasses. We plan to address this in some future release.

Using a Custom Style

There are several ways of using a custom style in a Qt application. The simplest way is to pass the custom style to the QApplication::setStyle() static function before creating the QApplication object:

You can call QApplication::setStyle() at any time, but by calling it before the constructor, you ensure that the user's preference, set using the -style command-line option, is respected.

You may want to make your custom style available for use in other applications, which may not be yours and hence not available for you to recompile. The Qt Plugin system makes it possible to create styles as plugins. Styles created as plugins are loaded as shared objects at runtime by Qt itself. Please refer to the Qt Plugin documentation for more information on how to go about creating a style plugin.

Compile your plugin and put it into Qt's plugins/styles directory. We now have a pluggable style that Qt can load automatically. To use your new style with existing applications, simply start the application with the following argument:

./myapplication -style custom

The application will use the look and feel from the custom style you implemented.

Right-to-Left Desktops

Languages written from right to left (such as Arabic and Hebrew) usually also mirror the whole layout of widgets, and require the light to come from the screen's top-right corner instead of top-left.

If you create a custom style, you should take special care when drawing asymmetric elements to make sure that they also look correct in a mirrored layout. An easy way to test your styles is to run applications with the -reverse command-line option or to call QApplication::setLayoutDirection() in your main() function.

Here are some things to keep in mind when making a style work well in a right-to-left environment:

When QStyledItemDelegate paints its items, it draws CE_ItemViewItem, and calculates their size with CT_ItemViewItem. Note also that it uses SE_ItemViewItemText to set the size of editors. When implementing a style to customize drawing of item views, you need to check the implementation of QCommonStyle (and any other subclasses from which your style inherits). This way, you find out which and how other style elements are painted, and you can then reimplement the painting of elements that should be drawn differently.

We include a small example where we customize the drawing of item backgrounds.

To add support for drawing of new datatypes and item data roles, it is necessary to create a custom delegate. But if you only need to support the datatypes implemented by the default delegate, a custom style does not need an accompanying delegate. The QStyledItemDelegate class description gives more information on custom delegates.

The drawing of item view headers is also done by the style, giving control over size of header items and row and column sizes.

The maximum allowed distance between the mouse and a scrollbar when dragging. Exceeding the specified distance will cause the slider to jump back to the original position; a value of -1 disables this behavior.

QStyle::PM_ScrollBarExtent

9

Width of a vertical scroll bar and the height of a horizontal scroll bar.

QStyle::PM_ScrollBarSliderMin

10

The minimum height of a vertical scroll bar's slider and the minimum width of a horizontal scroll bar's slider.

QStyle::PM_SliderThickness

11

Total slider thickness.

QStyle::PM_SliderControlThickness

12

Thickness of the slider handle.

QStyle::PM_SliderLength

13

Length of the slider.

QStyle::PM_SliderTickmarkOffset

14

The offset between the tickmarks and the slider.

QStyle::PM_SliderSpaceAvailable

15

The available space for the slider to move.

QStyle::PM_DockWidgetSeparatorExtent

16

Width of a separator in a horizontal dock window and the height of a separator in a vertical dock window.

QStyle::PM_DockWidgetHandleExtent

17

Width of the handle in a horizontal dock window and the height of the handle in a vertical dock window.

QStyle::PM_DockWidgetFrameWidth

18

Frame width of a dock window.

QStyle::PM_DockWidgetTitleMargin

?

Margin of the dock window title.

QStyle::PM_MenuBarPanelWidth

33

Frame width of a menu bar, defaults to PM_DefaultFrameWidth.

QStyle::PM_MenuBarItemSpacing

34

Spacing between menu bar items.

QStyle::PM_MenuBarHMargin

36

Spacing between menu bar items and left/right of bar.

QStyle::PM_MenuBarVMargin

35

Spacing between menu bar items and top/bottom of bar.

QStyle::PM_ToolBarFrameWidth

?

Width of the frame around toolbars.

QStyle::PM_ToolBarHandleExtent

?

Width of a toolbar handle in a horizontal toolbar and the height of the handle in a vertical toolbar.

QStyle::PM_ToolBarItemMargin

?

Spacing between the toolbar frame and the items.

QStyle::PM_ToolBarItemSpacing

?

Spacing between toolbar items.

QStyle::PM_ToolBarSeparatorExtent

?

Width of a toolbar separator in a horizontal toolbar and the height of a separator in a vertical toolbar.

QStyle::PM_ToolBarExtensionExtent

?

Width of a toolbar extension button in a horizontal toolbar and the height of the button in a vertical toolbar.

QStyle::PM_TabBarTabOverlap

19

Number of pixels the tabs should overlap. (Currently only used in styles, not inside of QTabBar)

enum QStyle::StyleHint

This enum describes the available style hints. A style hint is a general look and/or feel hint.

Constant

Value

Description

QStyle::SH_EtchDisabledText

0

Disabled text is "etched" as it is on Windows.

QStyle::SH_DitherDisabledText

1

Disabled text is dithered as it is on Motif.

QStyle::SH_GUIStyle

0x00000100

The GUI style to use.

QStyle::SH_ScrollBar_ContextMenu

?

Whether or not a scroll bar has a context menu.

QStyle::SH_ScrollBar_MiddleClickAbsolutePosition

2

A boolean value. If true, middle clicking on a scroll bar causes the slider to jump to that position. If false, middle clicking is ignored.

QStyle::SH_ScrollBar_LeftClickAbsolutePosition

?

A boolean value. If true, left clicking on a scroll bar causes the slider to jump to that position. If false, left clicking will behave as appropriate for each control.

QStyle::SH_ScrollBar_ScrollWhenPointerLeavesControl

3

A boolean value. If true, when clicking a scroll bar SubControl, holding the mouse button down and moving the pointer outside the SubControl, the scroll bar continues to scroll. If false, the scollbar stops scrolling when the pointer leaves the SubControl.

QStyle::SH_ScrollBar_RollBetweenButtons

?

A boolean value. If true, when clicking a scroll bar button (SC_ScrollBarAddLine or SC_ScrollBarSubLine) and dragging over to the opposite button (rolling) will press the new button and release the old one. When it is false, the original button is released and nothing happens (like a push button).

An integer indicating the opacity for the tip label, 0 is completely transparent, 255 is completely opaque.

QStyle::SH_DrawMenuBarSeparator

?

Indicates whether or not the menu bar draws separators.

QStyle::SH_TitleBar_ModifyNotification

?

Indicates if the title bar should show a '*' for windows that are modified.

QStyle::SH_Button_FocusPolicy

?

The default focus policy for buttons.

QStyle::SH_CustomBase

0xf0000000

Base value for custom style hints. Custom values must be greater than this value.

QStyle::SH_MenuBar_DismissOnSecondClick

?

A boolean indicating if a menu in the menu bar should be dismissed when it is clicked on a second time. (Example: Clicking and releasing on the File Menu in a menu bar and then immediately clicking on the File Menu again.)

QStyle::SH_MessageBox_UseBorderForButtonSpacing

?

A boolean indicating what the to use the border of the buttons (computed as half the button height) for the spacing of the button in a message box.

QStyle::SH_MessageBox_CenterButtons

?

A boolean indicating whether the buttons in the message box should be centered or not (see QDialogButtonBox::setCentered()).

QStyle::SH_MessageBox_TextInteractionFlags

?

A boolean indicating if the text in a message box should allow user interfactions (e.g. selection) or not.

QStyle::SH_TitleBar_AutoRaise

?

A boolean indicating whether controls on a title bar ought to update when the mouse is over them.

QStyle::SH_ToolButton_PopupDelay

?

An int indicating the popup delay in milliseconds for menus attached to tool buttons.

QStyle::SH_FocusFrame_Mask

?

The mask of the focus frame.

QStyle::SH_RubberBand_Mask

?

The mask of the rubber band.

QStyle::SH_WindowFrame_Mask

?

The mask of the window frame.

QStyle::SH_SpinControls_DisableOnBounds

?

Determines if the spin controls will shown as disabled when reaching the spin range boundary.

Returns the spacing that should be used between controls1 and controls2 in a layout. orientation specifies whether the controls are laid out side by side or stacked vertically. The option parameter can be used to pass extra information about the parent widget. The widget parameter is optional and can also be used if option is 0.

controls1 and controls2 are OR-combination of zero or more control types.

Draws the given control using the provided painter with the style options specified by option.

The widget argument is optional and can be used as aid in drawing the control.

The option parameter is a pointer to a QStyleOptionComplex object that can be cast to the correct subclass using the qstyleoption_cast() function. Note that the rect member of the specified option must be in logical coordinates. Reimplementations of this function should use visualRect() to change the logical coordinates into screen coordinates before calling the drawPrimitive() or drawControl() function.

The table below is listing the complex control elements and their associated style option subclass. The style options contain all the parameters required to draw the controls, including QStyleOption::state which holds the style flags that are used when drawing. The table also describes which flags that are set when casting the given option to the appropriate subclass.

Draws the given element with the provided painter with the style options specified by option.

The widget argument is optional and can be used as aid in drawing the control. The option parameter is a pointer to a QStyleOption object that can be cast to the correct subclass using the qstyleoption_cast() function.

The table below is listing the control elements and their associated style option subclass. The style options contain all the parameters required to draw the controls, including QStyleOption::state which holds the style flags that are used when drawing. The table also describes which flags that are set when casting the given option to the appropriate subclass.

Note that if a control element is not listed here, it is because it uses a plain QStyleOption object.

Draws the given text in the specified rectangle using the provided painter and palette.

The text is drawn using the painter's pen, and aligned and wrapped according to the specified alignment. If an explicit textRole is specified, the text is drawn using the palette's color for the given role. The enabled parameter indicates whether or not the item is enabled; when reimplementing this function, the enabled parameter should influence how the item is drawn.

Draws the given primitive element with the provided painter using the style options specified by option.

The widget argument is optional and may contain a widget that may aid in drawing the primitive element.

The table below is listing the primitive elements and their associated style option subclasses. The style options contain all the parameters required to draw the elements, including QStyleOption::state which holds the style flags that are used when drawing. The table also describes which flags that are set when casting the given option to the appropriate subclass.

Note that if a primitive element is not listed here, it is because it uses a plain QStyleOption object.

Returns the sub control at the given position in the given complex control (with the style options specified by option).

Note that the position is expressed in screen coordinates.

The option argument is a pointer to a QStyleOptionComplex object (or one of its subclasses). The object can be cast to the appropriate type using the qstyleoption_cast() function. See drawComplexControl() for details. The widget argument is optional and can contain additional information for the function.

Returns the area within the given rectangle in which to draw the provided text according to the specified font metrics and alignment. The enabled parameter indicates whether or not the associated item is enabled.

If the given rectangle is larger than the area needed to render the text, the rectangle that is returned will be offset within rectangle according to the specified alignment. For example, if alignment is Qt::AlignCenter, the returned rectangle will be centered within rectangle. If the given rectangle is smaller than the area needed, the returned rectangle will be the smallest rectangle large enough to render the text.

Returns the spacing that should be used between control1 and control2 in a layout. orientation specifies whether the controls are laid out side by side or stacked vertically. The option parameter can be used to pass extra information about the parent widget. The widget parameter is optional and can also be used if option is 0.

This slot is called by layoutSpacing() to determine the spacing that should be used between control1 and control2 in a layout. orientation specifies whether the controls are laid out side by side or stacked vertically. The option parameter can be used to pass extra information about the parent widget. The widget parameter is optional and can also be used if option is 0.

The specified option and widget can be used for calculating the metric. In general, the widget argument is not used. The option can be cast to the appropriate type using the qstyleoption_cast() function. Note that the option may be zero even for PixelMetrics that can make use of it. See the table below for the appropriate option casts:

Some pixel metrics are called from widgets and some are only called internally by the style. If the metric is not called by a widget, it is the discretion of the style author to make use of it. For some styles, this may not be appropriate.

This function is called for every widget at some point after it has been fully created but just before it is shown for the very first time.

Note that the default implementation does nothing. Reasonable actions in this function might be to call the QWidget::setBackgroundMode() function for the widget. Do not use the function to set, for example, the geometry. Reimplementing this function provides a back-door through which the appearance of a widget can be changed, but with Qt's style engine it is rarely necessary to implement this function; reimplement drawItemPixmap(), drawItemText(), drawPrimitive(), etc. instead.

The QWidget::inherits() function may provide enough information to allow class-specific customizations. But because new QStyle subclasses are expected to work reasonably with all current and future widgets, limited use of hard-coded customization is recommended.

Returns the size of the element described by the specified option and type, based on the provided contentsSize.

The option argument is a pointer to a QStyleOption or one of its subclasses. The option can be cast to the appropriate type using the qstyleoption_cast() function. The widget is an optional argument and can contain extra information used for calculating the size.

The standardIcon is a standard pixmap which can follow some existing GUI style or guideline. The option argument can be used to pass extra information required when defining the appropriate icon. The widget argument is optional and can also be used to aid the determination of the icon.

Warning: Because of binary compatibility constraints, this function is not virtual. If you want to provide your own icons in a QStyle subclass, reimplement the standardIconImplementation() slot in your subclass instead. The standardIcon() function will dynamically detect the slot and call it.

Reimplement this slot to provide your own icons in a QStyle subclass; because of binary compatibility constraints, the standardIcon() function (introduced in Qt 4.1) is not virtual. Instead, standardIcon() will dynamically detect and call this slot.

The standardIcon is a standard pixmap which can follow some existing GUI style or guideline. The option argument can be used to pass extra information required when defining the appropriate icon. The widget argument is optional and can also be used to aid the determination of the icon.

Note that on systems that support system colors, the style's standard palette is not used. In particular, the Windows XP, Vista, and Mac styles do not use the standard palette, but make use of native theme engines. With these styles, you should not set the palette with QApplication::setStandardPalette().

Returns the rectangle containing the specified subControl of the given complex control (with the style specified by option). The rectangle is defined in screen coordinates.

The option argument is a pointer to QStyleOptionComplex or one of its subclasses, and can be cast to the appropriate type using the qstyleoption_cast() function. See drawComplexControl() for details. The widget is optional and can contain additional information for the function.

Returns the sub-area for the given element as described in the provided style option. The returned rectangle is defined in screen coordinates.

The widget argument is optional and can be used to aid determining the area. The QStyleOption object can be cast to the appropriate type using the qstyleoption_cast() function. See the table below for the appropriate option casts:

This function is the counterpart to polish(). It is called for every polished widget whenever the style is dynamically changed; the former style has to unpolish its settings before the new style can polish them again.

Note that unpolish() will only be called if the widget is destroyed. This can cause problems in some cases, e.g, if you remove a widget from the UI, cache it, and then reinsert it after the style has changed; some of Qt's classes cache their widgets.