|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QStyleOption Class Reference
[QtGui module]

The QStyleOption class stores the parameters used by QStyle functions. More...

    #include <QStyleOption>

Inherited by QStyleOptionButton, QStyleOptionComplex, QStyleOptionDockWidget, QStyleOptionFocusRect, QStyleOptionFrame, QStyleOptionHeader, QStyleOptionMenuItem, QStyleOptionProgressBar, QStyleOptionQ3DockWindow, QStyleOptionQ3ListViewItem, QStyleOptionRubberBand, QStyleOptionTab, QStyleOptionTabWidgetFrame, QStyleOptionToolBox, and QStyleOptionViewItem.

- List of all members, including inherited members

### Public Types

- enum OptionType { SO_Default, SO_FocusRect, SO_Button, SO_Tab, ..., SO_ComplexCustomBase }

### Public Functions

- QStyleOption ( int version = QStyleOption::Version, int type = SO_Default )
- QStyleOption ( const QStyleOption & other )
- ~QStyleOption ()
- void init ( const QWidget * widget )
- QStyleOption & operator= ( const QStyleOption & other )

### Public Variables

- Qt::LayoutDirection direction
- QFontMetrics fontMetrics
- QPalette palette
- QRect rect
- QStyle::State state
- int type
- int version

### Static Public Members

- const int Type
- const int Version

### Related Non-Members

- T qstyleoption_cast ( const QStyleOption * option )
- T qstyleoption_cast ( QStyleOption * option )

## Detailed Description

The QStyleOption class stores the parameters used by QStyle functions.

QStyleOption and its subclasses contain all the information that QStyle functions need to draw a graphical element.

For performance reasons, there are few member functions and the access to the member variables is direct (i.e., using the `.` or `->` operator). This low-level feel makes the structures straightforward to use and emphasizes that these are simply parameters used by the style functions.

The caller of a QStyle function usually creates QStyleOption objects on the stack. This combined with Qt's extensive use of implicit sharing for types such as QString, QPalette, and QColor ensures that no memory allocation needlessly takes place.

The following code snippet shows how to use a specific QStyleOption subclass to paint a push button:

        void MyPushButton::paintEvent()
        {
            QStyleOptionButton option;
            option.init(this);
            option.state = isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
            if (isDefault())
                option.features |= QStyleOptionButton::DefaultButton;
            option.text = text();
            option.icon = icon();

            QPainter painter(this);
            style().drawControl(QStyle::CE_PushButton, &option, &painter, this);
        }

In our example, the control is a QStyle::CE_PushButton, and according to the QStyle::drawControl() documentation the corresponding class is QStyleOptionButton.

When reimplementing QStyle functions that take a QStyleOption parameter, you often need to cast the QStyleOption to a subclass. For safety, you can use qstyleoption_cast<T>() to ensure that the pointer type is correct. For example:

        void MyStyle::drawPrimitive(PrimitiveElement element,
                                    const QStyleOption *option,
                                    QPainter *painter,
                                    const QWidget *widget)
        {
            if (element == PE_FocusRect) {
                const QStyleOptionFocusRect *focusRectOption =
                        qstyleoption_cast<const QStyleOptionFocusRect *>(option);
                if (focusRectOption) {
                    ...
                }
            } else {
                ...
            }
        }

qstyleoption_cast<T>() will return 0 if the object to which `option` points isn't of the correct type.

See also QStyle and QStylePainter.

## Member Type Documentation

### enum QStyleOption::OptionType

This enum is used internally by QStyleOption, its subclasses, and qstyleoption_cast() to determine the type of style option. In general you do not need to worry about this unless you want to create your own QStyleOption subclass and your own styles.

Constant | Value | Description
--- | --- | ---
`QStyleOption::SO_Default` | `0` | QStyleOption
`QStyleOption::SO_FocusRect` | `1` | QStyleOptionFocusRect
`QStyleOption::SO_Button` | `2` | QStyleOptionButton
`QStyleOption::SO_Tab` | `3` | QStyleOptionTab
`QStyleOption::SO_TabWidgetFrame` | `13` | QStyleOptionTabBarBase
`QStyleOption::SO_TabBarBase` | `14` | QStyleOptionTabWidgetFrame
`QStyleOption::SO_MenuItem` | `4` | QStyleOptionMenuItem
`QStyleOption::SO_Complex` | `0xf0000` | QStyleOptionComplex
`QStyleOption::SO_Slider` |  | QStyleOptionSlider
`QStyleOption::SO_Frame` | `5` | QStyleOptionFrame
`QStyleOption::SO_ProgressBar` | `6` | QStyleOptionProgressBar
`QStyleOption::SO_Q3ListView` |  | QStyleOptionQ3ListView
`QStyleOption::SO_Q3ListViewItem` | `11` | QStyleOptionQ3ListViewItem
`QStyleOption::SO_Header` | `8` | QStyleOptionHeader
`QStyleOption::SO_Q3DockWindow` | `9` | QStyleOptionQ3DockWindow
`QStyleOption::SO_DockWidget` | `10` | QStyleOptionDockWidget
`QStyleOption::SO_SpinBox` |  | QStyleOptionSpinBox
`QStyleOption::SO_ToolButton` |  | QStyleOptionToolButton
`QStyleOption::SO_ComboBox` |  | QStyleOptionComboBox
`QStyleOption::SO_ToolBox` | `7` | QStyleOptionToolBox
`QStyleOption::SO_RubberBand` | `15` | QStyleOptionRubberBand
`QStyleOption::SO_TitleBar` |  | QStyleOptionTitleBar
`QStyleOption::SO_ViewItem` | `12` | QStyleOptionViewItem (used in Interviews)
`QStyleOption::SO_CustomBase` | `0xf00` | Reserved for custom QStyleOptions; all custom controls values must be above this value
`QStyleOption::SO_ComplexCustomBase` | `0xf000000` | Reserved for custom QStyleOptions; all custom complex controls values must be above this value

## Member Function Documentation

### QStyleOption::QStyleOption ( int version = QStyleOption::Version, int type = SO_Default )

Constructs a QStyleOption with version version and type type.

The version has no special meaning for QStyleOption; it can be used by subclasses to distinguish between different version of the same option type.

The state member variable is initialized to QStyle::State_None.

See also version and type.

### QStyleOption::QStyleOption ( const QStyleOption & other )

Constructs a copy of other.

### QStyleOption::~QStyleOption ()

Destroys the style option object.

### void QStyleOption::init ( const QWidget * widget )

Initializes the state, direction, rect, palette, and fontMetrics member variables based on widget.

This function is provided only for convenience. You can also initialize the variables manually if you want.

### QStyleOption & QStyleOption::operator= ( const QStyleOption & other )

Assign other to this QStyleOption.

## Member Variable Documentation

### const int QStyleOption::Type

Equals SO_Default.

### const int QStyleOption::Version

Equals 1.

### Qt::LayoutDirection QStyleOption::direction

This variable holds the text layout direction that should be used when drawing text in the control.

### QFontMetrics QStyleOption::fontMetrics

This variable holds the font metrics that should be used when drawing text in the control.

### QPalette QStyleOption::palette

This variable holds the palette that should be used when painting the control.

### QRect QStyleOption::rect

This variable holds the area that should be used for various calculations and painting.

This can have different meanings for different types of elements. For example, for QStyle::CE_PushButton it would be the rectangle for the entire button, while for QStyle::CE_PushButtonLabel it would be just the area for the push button label.

### QStyle::State QStyleOption::state

This variable holds the style flags that are used when drawing the control.

See also QStyle::drawPrimitive(), QStyle::drawControl(), QStyle::drawComplexControl(), and QStyle::State.

### int QStyleOption::type

This variable holds the option type of the style option.

See also OptionType.

### int QStyleOption::version

This variable holds the version of the style option.

This value can be used by subclasses to implement extensions without breaking compatibility. If you use qstyleoption_cast<T>(), you normally don't need to check it.

## Related Non-Members

### T qstyleoption_cast ( const QStyleOption * option )

Returns a T or 0 depending on the type and version of option.

Example:

        void MyStyle::drawPrimitive(PrimitiveElement element,
                                    const QStyleOption *option,
                                    QPainter *painter,
                                    const QWidget *widget)
        {
            if (element == PE_FocusRect) {
                const QStyleOptionFocusRect *focusRectOption =
                        qstyleoption_cast<const QStyleOptionFocusRect *>(option);
                if (focusRectOption) {
                    ...
                }
            }
            ...
        }

See also QStyleOption::type and QStyleOption::version.

### T qstyleoption_cast ( QStyleOption * option )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a T or 0 depending on the type of option.