[PyQt] How to cast QStyleOption objects in PyQt4?

Jugdish jugdizh at gmail.com
Tue Mar 2 17:01:26 GMT 2010

How do I convert a QStyleOptionViewItem into a QStyleOptionViewItemV4? Qt
has the qstyleoption_cast() method, but no such method exists in PyQt4.

I have my own subclass of QStyledItemDelegate and have overridden the
initStyleOption() method so that I can provide my own foreground/background
colors, fonts, etc. The problem I'm running into is with the background
color -- I need to set the backgroundBrush attribute, which is only
available on QStyleOptionViewItemV4. So I need to somehow cast the
QStyleOptionViewItem object into a QStyleOptionViewItemV4 object...

from PyQt4 import *

class MyItemDelegate(QStyledItemDelegate):

    def initStyleOption(self, option, index):
        QStyledItemDelegate.initStyleOption(self, option, index)

        # set font family and color
        option.palette.setBrush(QPalette.Text, QtCore.Qt.red)

        # set background color
        # this doesn't work:
        option.backgroundBrush = QBrush(QtCore.Qt.black)
        # neither does this:
        option = QStyleOptionViewItemV4(option)
        option.backgroundBrush = QBrush(QtCore.Qt.black)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100302/373945cc/attachment.html>

More information about the PyQt mailing list