chouqud
30th January 2014, 15:05
I've subclassed a QStyledItemDelegate with the goal to draw a pixmap in the centre of a QTableView’s cell. Our pixmap does get rendered correctly but we lose all style defined in the stylesheet. Removing the delegate restores the style.
I would like to understand how to use a QStyledItemDelegate while maintaining the existing style. Here’s a PySide delegate that I thought would simply render the cell as is. Applying this delegate removes the style.
class MyStyleDelegate(QtGui.QStyledItemDelegate):
def __init__(self, parent=None):
super(MyStyleDelegate, self).__init__(parent)
def paint(self, painter, option, index):
super(MyStyleDelegate, self).paint(painter, option, index)
modelIndex = index
model = index.model()
if isinstance(model, QtGui.QAbstractProxyModel):
modelIndex = model.mapToSource(index)
options = QtGui.QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
style = options.widget.style() if options.widget else QtGui.QApplication.style()
style.drawControl(style.CE_ItemViewItem, options, painter, options.widget)
QtGui.QStyledItemDelegate.paint(self, painter, option, modelIndex)
When I step through this code, our widget is always None. That seems to contradict all the examples that I've found.
We're also differing from the examples and using a QTableView with a proxymodel but from what I've read that should not affect the style.
Any help would be welcomed.
(This has been cross-posted from a colleague's post on http://qt-project.org/forums/viewthread/37757/. We have yet to decide which is the best resource for our PySide questions.)
I would like to understand how to use a QStyledItemDelegate while maintaining the existing style. Here’s a PySide delegate that I thought would simply render the cell as is. Applying this delegate removes the style.
class MyStyleDelegate(QtGui.QStyledItemDelegate):
def __init__(self, parent=None):
super(MyStyleDelegate, self).__init__(parent)
def paint(self, painter, option, index):
super(MyStyleDelegate, self).paint(painter, option, index)
modelIndex = index
model = index.model()
if isinstance(model, QtGui.QAbstractProxyModel):
modelIndex = model.mapToSource(index)
options = QtGui.QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
style = options.widget.style() if options.widget else QtGui.QApplication.style()
style.drawControl(style.CE_ItemViewItem, options, painter, options.widget)
QtGui.QStyledItemDelegate.paint(self, painter, option, modelIndex)
When I step through this code, our widget is always None. That seems to contradict all the examples that I've found.
We're also differing from the examples and using a QTableView with a proxymodel but from what I've read that should not affect the style.
Any help would be welcomed.
(This has been cross-posted from a colleague's post on http://qt-project.org/forums/viewthread/37757/. We have yet to decide which is the best resource for our PySide questions.)