Skip to content

PythonQt.QtGui.QStyleOption

QStyleOption

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

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.

Source: https://doc.qt.io/archives/qt-5.15/qstyleoption.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
OptionType attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
SO_Button attribute Any
SO_ComboBox attribute Any
SO_Complex attribute Any
SO_ComplexCustomBase attribute Any
SO_CustomBase attribute Any
SO_Default attribute Any
SO_DockWidget attribute Any
SO_FocusRect attribute Any
SO_Frame attribute Any
SO_GraphicsItem attribute Any
SO_GroupBox attribute Any
SO_Header attribute Any
SO_MenuItem attribute Any
SO_ProgressBar attribute Any
SO_RubberBand attribute Any
SO_SizeGrip attribute Any
SO_Slider attribute Any
SO_SpinBox attribute Any
SO_Tab attribute Any
SO_TabBarBase attribute Any
SO_TabWidgetFrame attribute Any
SO_TitleBar attribute Any
SO_ToolBar attribute Any
SO_ToolBox attribute Any
SO_ToolButton attribute Any
SO_ViewItem attribute Any
StyleOptionType attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
StyleOptionVersion attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
Type attribute Any (shared captured text)
Version attribute Any (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
direction attribute Any (shared captured text)
fontMetrics attribute QFontMetrics
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
initFrom function (self, w: Any) -> None Initializes the [state](https://doc.qt.io/archives/qt-5.15/qstyleoption.html#state-var), [direction](https://doc.qt.io/archives/qt-5.15/qstyleoption.html#direction-var),…
palette attribute QPalette (shared captured text)
rect attribute QRect (shared captured text)
state attribute Any (shared captured text)
styleObject attribute QObject
type attribute Any (shared captured text)
version attribute Any (shared captured text)