<br><div class="gmail_quote">Hi there,<br><br>I'm programming a small finance application and I use QTableViews intensively. It happens that for most of these tables I have special painting requirements and need a custom delegate. I am using QtyledItemDelegate as a base class. One of the things I would like the delegate to manage is the painting of alternate colors for each row.<br>
<br>To achieve that, I used the option parameter of the paint method. This object is a QStyleOptionViewItem from which a QStyleOptionViewItemV2 can be created and initiated. The features property of the QStyleOptionViewItemV2 would tell me if I am painting an alternate color or not provided the alternatingRowColors property of the QTableView is set to True. This way I don't need to implement any alternate color mechanism in my code, I merely detect it and choose a color accordingly.<br>
<br>The code would be something like;<br><br><span style="font-family: courier new,monospace;"> def paint(self, painter, option, index):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> style = QtGui.QStyleOptionViewItemV2(option)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> self.initStyleOption(style, index)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> painter.save()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if style.state & QtGui.QStyle.State_Selected:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> painter.fillRect(option.rect, option.palette.highlight().color())</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> painter.setPen(option.palette.highlightedText().color())</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> elif style.features & QtGui.QStyleOptionViewItemV2.Alternate:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> painter.fillRect(option.rect, QtGui.QColor(246,255,218))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> else:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> painter.fillRect(option.rect, QtGui.QColor(191,222,185))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> QtGui.QStyledItemDelegate.paint(self, painter, option, index)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> painter.restore()</span><br><br>However at the moment and since I upgraded to PyQT 4.7.3, the style.features property is not initiated properly and the code is always falling on the else statement, which means no alternate colors.<br>
<br>So my question is if the above code should behave like I was expecting or not. It definitely worked before.<br><br>But if not then what is the best way to implement this? Having a custom role that I can pass to the data method of the model to detect if I am painting an alternate row, or perhaps just use the index.row() to detect even and odd row numbers, or even use a Style?<br>
<br>Cheers,<br><br clear="all">--<br><font color="#888888">Ricardo Cruz<br>
</font></div><br>