|  | Home · Overviews · Examples

# QStyleOption Class Reference
[com.trolltech.qt.gui module]

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

Inherited by `QStyleOptionButton`, `QStyleOptionComplex`, `QStyleOptionDockWidget`, `QStyleOptionFocusRect`, `QStyleOptionFrame`, `QStyleOptionGraphicsItem`, `QStyleOptionHeader`, `QStyleOptionMenuItem`, `QStyleOptionProgressBar`, `QStyleOptionRubberBand`, `QStyleOptionTab`, `QStyleOptionTabBarBase`, `QStyleOptionTabWidgetFrame`, `QStyleOptionToolBar`, `QStyleOptionToolBox`, and `QStyleOptionViewItem`.

## 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(QPaintEvent *)
        {
            QStyleOptionButton option;
            option.initFrom(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() 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_FrameFocusRect) {
                const QStyleOptionFocusRect *focusRectOption =
                        qstyleoption_cast<const QStyleOptionFocusRect *>(option);
                if (focusRectOption) {
                    // ...
                }
            }
            // ...
        }

The qstyleoption_cast() function will return 0 if the object to which `option` points is not of the correct type.

For an example demonstrating how style options can be used, see the Styles example.

See also `QStyle` and `QStylePainter`.