[PyQt] QStyledItemDelegate.paint ignores styles?

Gerard Brunick gbrunick at gmail.com
Sun May 1 18:02:27 BST 2011


If I override the paint method of QStyledItemDelegate and call the 
base case, then the base case seems to ignore styles.  I thought that 
the whole point of QStyledItemDelegate was that it handled styles 
correctly? PyQt4.QtCore.QT_VERSION_STR is '4.6.2' and 
PyQt4.QtCore.PYQT_VERSION_STR is '4.7.2'.  I'm also on Ubuntu 10.04.2 LTS.

A minimal example illustrating the problem follows.  If
override_paint == 0, then I see the styles, but if I set it to 1 or 2, 
the effects of the styles disappear.

######################################################################
# change this
override_paint = 0 # 0 or 1 or 2

import sys
from PyQt4 import QtCore, QtGui

class TestDelegate(QtGui.QStyledItemDelegate):
     if override_paint == 1:
         def paint(self, painter, option, index):
             QtGui.QStyledItemDelegate.paint(self, painter, option, index)

     if override_paint == 2:
         def paint(self, painter, option, index):
             super(TestDelegate, self ).paint(painter, option, index)

app = QtGui.QApplication(sys.argv)

# Make tableview
tableView = QtGui.QTableView()
tableView.setStyleSheet("""QTableView::item { padding: 10px;
                                               background-color: blue;
                                              }""")

# Make delegate
delegate = TestDelegate(tableView)
tableView.setItemDelegate(delegate)

# Make the model
model = QtGui.QStandardItemModel(4, 2)
for row in range(4):
     for column in range(2):
         index = model.index(row, column, QtCore.QModelIndex())
         model.setData(index, "\n".join(["(%i, %i)" % (row, column)]))
tableView.setModel(model)

tableView.show()
sys.exit(app.exec_())
#############################################################################

Is this a bug or am I missing something?  Thanks for any info.
Gerard


More information about the PyQt mailing list