class QStylePainter

The QStylePainter class is a convenience class for drawing QStyle elements inside a widget. More

Inheritance diagram of PySide6.QtWidgets.QStylePainter

Synopsis

Methods

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

QStylePainter extends QPainter with a set of high-level draw...() functions implemented on top of QStyle ‘s API. The advantage of using QStylePainter is that the parameter lists get considerably shorter. Whereas a QStyle object must be able to draw on any widget using any painter (because the application normally has one QStyle object shared by all widget), a QStylePainter is initialized with a widget, eliminating the need to specify the QWidget , the QPainter, and the QStyle for every function call.

Example using QStyle directly:

def paintEvent(self, */):

    painter = QPainter(self)
    option = QStyleOptionFocusRect()
    option.initFrom(self)
    option.backgroundColor = palette().color(QPalette.Background)
    style().drawPrimitive(QStyle.PE_FrameFocusRect, option, painter, self)

Example using QStylePainter :

def paintEvent(self, */):
painter = QStylePainter(self)
option = QStyleOptionFocusRect()
option.initFrom(self)
option.backgroundColor = palette().color(QPalette.Background)
painter.drawPrimitive(QStyle.PE_FrameFocusRect, option)

See also

QStyle QStyleOption

__init__()

Constructs a QStylePainter .

__init__(w)
Parameters:

wQWidget

Construct a QStylePainter using widget widget for its paint device.

__init__(pd, w)
Parameters:

Construct a QStylePainter using pd for its paint device, and attributes from widget.

begin(w)
Parameters:

wQWidget

Return type:

bool

Begin painting operations on the specified widget. Returns true if the painter is ready to use; otherwise returns false.

This is automatically called by the constructor that takes a QWidget .

begin(pd, w)
Parameters:
Return type:

bool

This is an overloaded function.

Begin painting operations on paint device pd as if it was widget.

This is automatically called by the constructor that takes a QPaintDevice and a QWidget .

drawComplexControl(cc, opt)
Parameters:

Use the widget’s style to draw a complex control cc specified by the QStyleOptionComplex option.

drawControl(ce, opt)
Parameters:

Use the widget’s style to draw a control element ce specified by QStyleOption option.

See also

drawControl()

drawItemPixmap(r, flags, pixmap)
Parameters:

Draws the pixmap in rectangle rect. The pixmap is aligned according to flags.

See also

drawItemPixmap() Alignment

drawItemText(r, flags, pal, enabled, text[, textRole=QPalette.NoRole])
Parameters:

Draws the text in rectangle rect and palette pal. The text is aligned and wrapped according to flags.

The pen color is specified with textRole. The enabled bool indicates whether or not the item is enabled; when reimplementing this bool should influence how the item is drawn.

See also

drawItemText() Alignment

drawPrimitive(pe, opt)
Parameters:

Use the widget’s style to draw a primitive element pe specified by QStyleOption option.

See also

drawPrimitive()

style()
Return type:

QStyle

Return the current style used by the QStylePainter .