[PyQt] How to cast QStyleOption objects in PyQt4?

Phil Thompson phil at riverbankcomputing.com
Tue Mar 2 18:17:05 GMT 2010


On Tue, 2 Mar 2010 17:01:26 +0000, Jugdish <jugdizh at gmail.com> wrote:
> 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.font.setFamily("Courier")
>         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)

Implemented in tonight's snapshot - but untested.

Phil


More information about the PyQt mailing list