[PyQt] QStyledItemDelegate.paint ignores styles?

Hans-Peter Jansen hpj at urpla.net
Wed May 4 20:56:03 BST 2011


On Sunday 01 May 2011, 19:02:27 Gerard Brunick wrote:
> 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.

You seem to suffer from an older bug, that I don't remember.

At least, with the following modules and overriding paint, the style is 
applied correctly to the items:
python: 2.6
sip: 4.12.2
qt4: 4.7.1
pyqt4: 4.8.4

Pete


More information about the PyQt mailing list