SoFunction
Updated on 2025-05-19

PyQt5 The specific use of QStyleOptionViewItem class

QStyleOptionViewItemYes is used in PyQt5 for custom view items (such asQListViewQTableViewThe core class of the item in  ) style. It inherits fromQStyleOption, encapsulates the information required for drawing view items (such as position, status, text, icon, etc.) for delegate class (QStyledItemDelegate) Used during rendering.

​1. Core functions

  • ​Transfer style information: including the position, status (select/hover/activate), text alignment, font, color, etc.
  • ​Support custom rendering:()Get and modify the style options in the method.
  • Platform consistency: Ensure that items are rendered correctly according to the current style rules under different operating systems.

​2. Main attributes

property type illustrate
index QModelIndex Index of items in the model (data roles such as )
rect QRect The drawing area (position and size) of the item
state The status of the item (such as State_Selected, State_MouseOver)
text str Display text of item
font QFont Font of item
backgroundBrush QBrush Background brush for items (color/gradient/texture)
textAlignment Text alignment (such as  )
icon QIcon Icons of items
decorationSize QSize The size of the icon
features Additional features of the item (such as whether to display a decorative icon)

​3. Common methods

1. Initialize the option object

def initFrom(widget: QWidget) -> None

The basic properties of the initialization style options (such as palettes, orientations) usually need to be called in a custom delegate.

Example:

option = QStyleOptionViewItem()
(self)  # Inherit basic styles from delegate objects

​2. Get/modify properties

Access or modify values ​​directly through the attribute name:

# Set text color(, QColor("red"))

# Get whether the item is selectedis_selected =  & QStyle.State_Selected

​4. Code example: Custom item rendering

​1. Custom delegate class

from  import QStyledItemDelegate, QStyleOptionViewItem
from  import QPainter, QColor, QFont
from  import Qt

class CustomDelegate(QStyledItemDelegate):
    def paint(self, painter, option, index):
        # Initialize style options        option = QStyleOptionViewItem(option)
        (option, index)

        # Modify style: Select the background color and font to thicken        if  & QStyle.State_Selected:
             = QColor("#E3F2FD")
            (True)

        # Call the parent class method to continue rendering (or fully customize drawing)        super().paint(painter, option, index)

2. Completely customize drawing

def paint(self, painter, option, index):
    # Initialize style options    option = QStyleOptionViewItem(option)
    (option, index)

    # Save brush status    ()

    # Draw the background    if  & QStyle.State_Selected:
        (, QColor("#E3F2FD"))
    else:
        (, )

    # Draw icons    icon_rect = (5, 5, -5, -5)
    icon_rect.setWidth(32)
    (painter, icon_rect, )

    # Draw text    text_rect = (40, 0, -5, 0)
    ()
    (())
    (text_rect, , )

    # Restore brush status    ()

​5. Key enumeration values

​1. Status flags

value illustrate
State_Enabled Item enabled
State_Selected The item is selected
State_MouseOver Hover over the title
State_HasFocus Item gains focus

​2.   Features

value illustrate
None No additional features
WrapText Text wrapping automatically
Alternate Alternating background colors (for zebra crossing effects)

​6. Things to note

  • Object life cycle:

    • QStyleOptionViewItemUsually inpaint()Created in the method without manually freeing memory.
  • Platform Differences:

    • Modifying the style options directly may undermine cross-platform consistency, and it is recommended to prioritize the standard method of delegates.
  • Performance optimization:

    • Avoidpaint()Frequently create complex objects and try to reuse resources.

​7. Application scenarios

  • Highlight specific items: Modify the background color or font according to data conditions (such as error status).
  • ​Custom layout: adjust the position and spacing of icons and text.
  • ​Complex interactive effects: implement hover animation and custom focus boxes.

passQStyleOptionViewItem, Developers can deeply control the rendering logic of view items and build a highly customized user interface.

This is the article about the specific use of the PyQt5 QStyleOptionViewItem class. For more related contents of PyQt5 QStyleOptionViewItem, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!