Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QStyleOption Class Reference

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

    #include <QStyleOption>

Inherited by QStyleOptionFocusRect, QStyleOptionFrame, QStyleOptionHeader, QStyleOptionButton, QStyleOptionTab, QStyleOptionProgressBar, QStyleOptionMenuItem, QStyleOptionListViewItem, QStyleOptionDockWindow, QStyleOptionViewItem, QStyleOptionToolBox, and QStyleOptionComplex.

- List of all members, including inherited members

### Public Types

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

### Read-Only Properties

palette : QPalette rect : QRect state : QStyle::SFlags | type : int version : int

### Public Functions

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

### Related Non-Members

- T qt_cast ( const QStyleOption * option )
- T qt_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 variables is direct. This "low-level" feel makes the structures use straightforward and emphasizes that these are simply parameters used by the style functions. As a downside, it forces developers to be careful to initialize all the variables.

Example:

        void MyStyle::drawPrimitive(PrimitiveElement element,
                                    const QStyleOption *option,
                                    QPainter *painter,
                                    const QWidget *widget)
        {
            QRect rect = option->rect;
            QPalette palette = option->palette;

            ...
        }

When reimplementing QStyle functions that take a QStyleOption parameter, you often need to cast the QStyleOption to a subclass (e.g., QStyleOptionFocusRect). For safety, you can use qt_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 =
                        qt_cast<const QStyleOptionFocusRect *>(option);
                if (focusRectOption) {
                    ...
                }
            } else {
                ...
            }
        }

See also QStyle.

## Member Type Documentation

### enum QStyleOption::OptionType

This enum is used internally by QStyleOption, its subclasses, and qt_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.

`QStyleOption::SO_Default` | QStyleOption
`QStyleOption::SO_FocusRect` | QStyleOptionFocusRect
`QStyleOption::SO_Button` | QStyleOptionButton
`QStyleOption::SO_Tab` | QStyleOptionTab
`QStyleOption::SO_MenuItem` | QStyleOptionMenuItem
`QStyleOption::SO_Complex` | QStyleOptionComplex
`QStyleOption::SO_Slider` | QStyleOptionSlider
`QStyleOption::SO_Frame` | QStyleOptionFrame
`QStyleOption::SO_ProgressBar` | QStyleOptionProgressBar
`QStyleOption::SO_ListView` | QStyleOptionListView
`QStyleOption::SO_ListViewItem` | QStyleOptionListViewItem
`QStyleOption::SO_Header` | QStyleOptionHeader
`QStyleOption::SO_DockWindow` | QStyleOptionDockWindow
`QStyleOption::SO_SpinBox` | QStyleOptionSpinBox
`QStyleOption::SO_ToolButton` | QStyleOptionToolButton
`QStyleOption::SO_ComboBox` | QStyleOptionComboBox
`QStyleOption::SO_ToolBox` | QStyleOptionToolBox
`QStyleOption::SO_TitleBar` | QStyleOptionTitleBar
`QStyleOption::SO_ViewItem` | QStyleOptionViewItem (used in Interviews)
`QStyleOption::SO_CustomBase` | Reserved for custom QStyleOptions; all custom controls values must be above this value
`QStyleOption::SO_ComplexCustomBase` | Reserved for custom QStyleOptions; all custom complex controls values must be above this value

## Property Documentation

### palette : QPalette

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

### rect : QRect

This property 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.

### state : QStyle::SFlags

This property holds the that are used when drawing the control.

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

### type : int

This property holds the OptionType of the .

### version : int

This property holds the version of the .

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

## 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::Style_None.

See also version and type.

### QStyleOption::~QStyleOption ()

Destroys the style option object.

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

Intializes the state, rect, and palette member variables based on widget.

This function is provided only for convenience. You can also initialize the state, rect, and palette variables manually if you want.

## Related Non-Members

### T qt_cast ( const 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 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 =
                        qt_cast<const QStyleOptionFocusRect *>(option);
                if (focusRectOption) {
                    ...
                }
            }
            ...
        }

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

### T qt_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.